r/functionalprogramming • u/wwwtrollfacecom • Jul 14 '24
Question How does memory allocation in functional languages differ from imperitive languages like, say, C?
Context: I'm pretty new to the functional game, and most of my experience has bene with statically typed imperative languages.
To elaborate on the question, how do functional languages handle memory allocation for recursive functions effectively? Take this C program that sums up integers in an array.
int arr = ;
int sum = 0;
for
(I hope the concept's clear here, because i'm not an ocaml guy)
I'd assume this is the defacto way of performing such operations in a functional language, so what makes it nearly as efficient as something like C?
22
Upvotes
25
u/Athas Jul 14 '24
That OCaml program does not need to allocate heap memory. The only data that is being constructed is integers, and most mature functional language implementations can embed small integers into the pointers themselves, to avoid boxing.
In general, functional languages implement memory management through standard garbage collection techniques (although exceptions exist, such as MLKit, which uses region inference).
Of course, there's also some fine print regarding your concrete examples:
The OCaml function you posted is not tail recursive, and so it does need to allocate stack memory for the recursive call to
sum. That is going to be significantly less efficient than the imperative loop you wrote in C. If you rewrite the OCaml function to be tail recursive instead, then it would likely be essentially equivalent in performance to the C function.Also, your C function uses an actual array, while the OCaml function traverses a linked list. The array is guaranteed to have excellent locality (ensuring efficient use of CPU caches), while the OCaml program may have to jump around in memory as it chases down the pointers to the next node. That will incur significant overhead (but no allocation, as the list exists before the function is called).
10
u/Syrak Jul 14 '24
One technique to eliminate intermediate data structures (trees) is called deforestation.
5
u/[deleted] Jul 14 '24
This is one of many ways how pure functional language implemented.
https://www.microsoft.com/en-us/research/wp-content/uploads/1987/01/slpj-book-1987-small.pdf
Chapter 17 is about Memory management.
5
u/imihnevich Jul 14 '24
In addition to what's been said, I recommend writing few simple programs and putting them into godbolt.org
It produces assembly and you can see what's going on
5
u/yawaramin Jul 14 '24
You can easily use imperative methods in OCaml and it's common when high performance is needed:
The nice thing about this is that it's easy to encapsulate this highly performant imperative code in a functional shell. Best of both worlds.