Hacker News new | ask | show | jobs
by hderms 2519 days ago
Imo you have to allocate something else if you are going with an immutable paradigm. You can do clever sharing of data structures, but ultimately if you're modifying the whole thing, you're modifying the whole thing and sharing can only go so far. It seems like further advances in this space either require giving the compiler a better understanding of what is "actually" shared and in what way (i.e. Rust and knowing when mutations are externally visible), or making copying cheaper.

Mutability is always (at least as current computer architectures are concerned) going to be faster in a general sense.

2 comments

You may be interested in the `im` crate for Rust, which implements efficient copy-on-write structural sharing, falling back to in-place mutation when there are no other outstanding references.

https://docs.rs/im/14.1.0/im/

> It seems like further advances in this space either require giving the compiler a better understanding of what is "actually" shared

In the context of arrays, it helps when programmers use higher-order combinators/functions, e.g. maps.

There is a mutable way to do a map, and an immutable way, and which should be used in the compiled program depends on analysis of where the variable is used.