Hacker News new | past | comments | ask | show | jobs | submit login

What is the connection between mutability and excessive hardware usage?



As an oversimplified example, in using an immutable screen buffer, a change between frames results in allocation of a full buffer rather than overwriting memory within the buffer (though one could probably think of ways to optimize this for the use case). Excess comes either in the form of unnecessary (relative to mutable) memory usage and/or CPU cycles necessary to support high-levels of garbage collection.


> in using an immutable screen buffer, a change between frames results in allocation of a full buffer rather than overwriting memory within the buffer

That’s actually not how clojure’s immutable data structures work. They use structural sharing, so only the portion that changes (roughly) needs new memory allocated, and only the parts that changed get garbage collected, so it is a bit more efficient than that.


You can't deallocate part of a frame buffer, it's a single allocation. Unless every pixel is individually allocated, which would clearly be insane.


But if your frame buffer is text, you can store the text in a rope data structure like JS vms do

So you don't necessarily have an allocation for every single character, but you're still able to share memory between buffers

I've implemented a game engine with immutability (makes for fast cloning in AI search) where much of the game state is shared between clones. With reference counting it also means if there's a unique reference being modified then no copy is made. This same trick is used by Python to optimize string concatenation


I'm explaining how clojure's data structures are different from OP's screen buffer example.


that's only because js is "stupid"

if you have the string "hello" and somewhere else "hello world" you can retain only one copy of "hello" because it's guaranteed it won't change.

but js vm it's not smart enough for that.


You're wrong, JS has immutable strings & so VMs use ropes to make mutable usage & slicing fast

https://gist.github.com/mraleph/3397008 https://twitter.com/rauschma/status/1269964154275799041


thanks

my bad, I learned something new.

anyway this proves that immutable data structures are not inherently slow, this is infact an optimization that makes things faster.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: