English3000

English3000

According to GeeksForGeeks, “Stack space in recursive calls counts too as extra space required by a program.”

In other words, to get the benefit of immutable data in memory, that requires a lot of temporarily-used extra memory (as compared with a mutable language).

Add in garbage collection of the heap to clear up this space.

In what use cases is there actually a tradeoff here?

Garbage collection happens more often. Erlang’s design handles potential issues there, right?

So then, only when dealing with large inputs could that extra temporarily-used space lead to issues.

Is that right? Just considering this out loud.

Showing Posts 1 to 10

sneako

sneako

Elixir has tail call optimization, so if the last thing a recursive function does, is call itself the compiler can optimize this. Here’s a blog post about it Tail Call Optimization with Fibonacci in Elixir | Stride

English3000

English3000 OP

Found this: “Tail-call optimization is where you are able to avoid allocating a new stack frame for a function because the calling function will simply return the value that it gets from the called function.”

So then functional programmers made this consideration. Hence there is no tradeoff in memory usage.

I.e. a function that mutates its argument iteratively will use just about the same amount of space as one that handles its argument with tail-recursion because a tail-recursive function can drop each call from the call stack just as it recurses to the next call (because the final call returns the output, rather than the initial one).

In effect, this means that the memory allocated for inputs can be reused by tail-recursive calls, correct?

sneako

sneako

Yes, the memory allocated for a function call is reused with the new inputs

michalmuskala

michalmuskala

The way tail recursion is implemented in BEAM is quite interesting. Instead of automatically allocating stack frames when you enter a function and deallocating on return like many VMs do, in BEAM this stack management in manual - you get a allocate n instruction which allocates a stack frame of size n and a corresponding deallocate n instruction that does the reverse. This means that some functions that don’t need stack space don’t allocate anything at all in the first place, but other functions can deallocate their stack frame before calling the last function - this effectively is a tail call optimisation.

Additionally Erlang (and Elixir) implement a slightly more powerful version of tail recursion optimisation called last call optimisation, where not only recursive calls are optimised, but all calls in the “tail” or “last” position are optimised. This makes it trivial to tail-optimise mutually recursive functions (a calls b which calls a which calls b …), which is very hard or impossible in other schemes.

English3000

English3000 OP

So this gives more flexibility.

English3000

English3000 OP

Reading up a bit more, here’s my takeaway:

Software needs mutability in order to do things. The consideration for the programmer is which levels/layers of the software should and should not be mutable.

e.g. Elixir uses immutability but any database it uses will, by its function, be mutable.

StefanHoutzager

StefanHoutzager

Read “immutability changes everything”?

English3000

English3000 OP

Thanks.

My takeaway from this article is the increases in storage, scale, and distribution create issues which are dealt with by making more layers of an app immutable, including the database via versioning and hardware via wear leveling.

StefanHoutzager

StefanHoutzager

There are trade-off’s. But it is good to be able to make well-informed choices.

StefanHoutzager

StefanHoutzager

Already read about “functional core, imperative shell”/“onion architecture” and whatever names it has (hexagonal etc) Functional Core, Imperative Shell

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews