3 ms·
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
by cjcampbell 5y ago
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.
- peoplefromibiza 5y agothat'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.
- __s 5y agoYou're wrong, JS has immutable strings & so VMs use ropes to make mutable usage & slicing fast https://gist.github.com/mraleph/3397008 https://gist.github.com/mraleph/3397008 https://twitter.com/rauschma/status/1269964154275799041 https://twitter.com/rauschma/status/1269964154275799041
- peoplefromibiza 5y agothanks 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.
- knubie 5y ago> 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.
- ben-schaaf 5y agoYou can't deallocate part of a frame buffer, it's a single allocation. Unless every pixel is individually allocated, which would clearly be insane.
- knubie 5y agoI'm explaining how clojure's data structures are different from OP's screen buffer example.
- __s 5y agoBut 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