3 ms·
Immutability is a drag if you create lots and lots of referenced state because, e.g., you want to hold on to many snapshots of past state. Immutability done ri
by cryptonector 5y ago
Immutability is a drag if you create lots and lots of referenced state because, e.g., you want to hold on to many snapshots of past state.
Immutability done right need not be much worse than mutability.
For example, jq's internal value representation is immutable in appearance:
- mutations yield new values,
- but when the value being "mutated" has only one extant reference then mutation is done in-place,
- while when the value being "mutated" has more than one extant reference then mutation is done by copy-on-write.
If you manage to always have extra references, then "immutable mutation" gets expensive.
If you manage hold on to old references for a long time, then "immutable mutation" gets even more expensive.
In a run-time with a GC not based on reference counting you do have to GC all the intermediate garbage.
Immutable data structures really lend themselves well to reference counting GCs because you can't have cycles in immutable data.
- fnordsensei 5y ago> Immutability is a drag if you create lots and lots of referenced state because, e.g., you want to hold on to many snapshots of past state. Isn't it the opposite? If you want to hold on to many referenced states at the same time, an immutable data structure should provide less overhead than a mutable one, due to structural sharing.
- cryptonector 5y agoA performance drag. Let's say you have an app that for N inputs creates N^2 state in memory... But now, if that's just what you must do for some reason, then, yes, immutability makes the task of taking all those snapshots real easy.
- cryptonector 5y agoI should add that reference counting GCs are nice because short-lived garbage is freed immediately and there's no need to look for garbage, so they're much faster than scanning GCs. Reference counting GCs can have GC-like pauses when releasing objects that have singular references to many many many other objects, but the same is true in manual memory management systems.