So if you had to optimize for raw speed, why not choose mutable data?
So if you had to optimize for raw speed, why not choose mutable data?
Conceptually yes, but the implementation doesn't always necessarily need to work that way under the hood: https://www.roc-lang.org/functional#opportunistic-mutation
That's not generally true. Many immutable languages are using "persistent" data structures, where "persist" here means that much of the original structure persists in the new one.
For more, see:
- Purely Functional Data Structures by Okasaki: https://www.cs.cmu.edu/~rwh/students/okasaki.pdf - Phil Bagwell's research - e.g., https://infoscience.epfl.ch/record/64398/files/idealhashtree...
That is not true in general. There are plenty of data structures that can be updated without forcing a full copy. Lists, trees, sets, maps, etc. All of these are common in functional programming. This is discussed in the article (e.g. "Append-Only Computing").
>if you had to optimize for raw speed, why not choose mutable data?
So in context we are talking about a case where we have to optimize for raw speed.
It doesn’t matter that immutable data is easier to reason about if you don’t have the performance budget to go that route.
2. Of course given a sufficiently large array, iterating over it with 16 processors is faster than with 1. Arrays still dominate other data structures for raw performance here.
3. Concurrency doesn’t just mean multi threading. SIMD instructions can perform simultaneous operations on multiple operands in your array. Can’t do this with a linked list.
For a similar reason immutability also helps you write code with fewer data races.
The only reason you’d need to deal with increasing parallelism (beyond SIMD) is if you wanted it even faster than that.
I’m not saying immutable data isn’t a good idea in many cases (my primary day job language these days is Elixir). What I am saying is that if you are “optimizing for raw speed” immutable data structures are almost never the right choice.
That doesn’t mean immutable data structures can’t be fast enough to be the best choice in many situations.
Sometimes you actually do want multiple threads working with data.
You never want multiple threads to be mutating the same data without some form of synchronisation, but with ownership rules, you can still have that synchronisation.
while querying a database each transaction sees a snapshot of data (a database version) as it was some time ago, regardless of the current state of the underlying data
https://www.postgresql.org/docs/7.1/mvcc.html