3 ms·
Fascinated to see the mutable tuple optimization. Tuples are dense contiguous vectors in memory, which provide O(1) access - like binaries, but unlike lists. I
by mikhailfranco 3y ago
Fascinated to see the mutable tuple optimization. Tuples are dense contiguous vectors in memory, which provide O(1) access - like binaries, but unlike lists.
It would be amazing if tuples could also provide O(1) mutable update, in special situations when the original value is not accessed again.
The Release Notes show record syntax example.
Most tuple changes in code are done through pattern-matched deconstruction, then independent re-construction, which is impossible for the compiler to recognize.
So the first step to wider optimization would be common use of 'setelement', very well hidden in the erlang module (i.e. not 'set_element', and no Elixir 'Tuple.set' or 'Tuple.update_at').
Then later, that function would get optimized away in many situations. Given that tuples can be up to 60m elements, it would give Erlang a big boost to have O(1) mutable vectors (note the `array` module is a functional tree, not a mutable 1D vector).
I am thinking about graphics and data analysis - a huge win. Elixir could also benefit tremendously. Various packages, like 'array' module in Erlang, and 'Nx' in Elixir, would be radically changed. Maybe Elixir could extend '[i]' syntax to access tuples.
Let's go for OTP 28!