3 ms·
"One reason is RAM consumption" You can have immutability without data duplication; in an immutable-only environment, every process/thread/whatever that uses a
by yellowapple 10y ago
"One reason is RAM consumption"
You can have immutability without data duplication; in an immutable-only environment, every process/thread/whatever that uses a specific object can safely reuse a pointer to that object and always know that the object represented by that pointer cannot change under their noses. It's only when you need to "modify" that object (read: create a new copy) where you start to see RAM consumption issues, and even that can be mitigated.
Linked lists are an example of where this works remarkably well; if you have a list (E→D→C→B→A) and want to append F to that list, you can do so trivially without needing to mutate or copy the original (since all you need for the new list is F and a pointer to E). Same if you wanted to replace E with F; just have F point to D instead.
- Const-me 10y agoIt’s all good in theory, not in practice. > only when you need to "modify" that object Well, unless you’re modifying your data, almost all data structures are fine for multithreaded access. > Linked lists are an example of where this works remarkably well Linked lists are an example of what you don’t want to use if you care about performance even a tiny bit. On modern hardware, random memory access is slow. A ballpark estimate is 100-200 CPU cycles just to read a single value (16 bytes for dual-channel DDR) from RAM. With a linked list you pay that cost with each iteration to the next list element. Very expensive.