[ENH] Immutable objects
Brian Rice
water at tunes.org
Wed Mar 1 20:47:25 PST 2006
I've added a basic setup for the VM to support immutable objects. For
now, you call #immutably on an object and get a clone that cannot be
modified in any way. This can be done on any object or array and
works consistently. The userland code for this is in src/mobius/vm/
interp/map.slate.
In order to make a modified version of an immutable object, for now
there is #cloneSettingSlots:to: which was introduced a couple of
months ago for this purpose, but at that point was defined in
root.slate instead of as a VM primitive. It's in the VM now to bypass
the safety checks that #atSlotNamed:put: and #at:put: perform if the
object is marked immutable.
The only cost to calling #immutably is that the map for the object is
copied with a new flag value for mutability, so it has the same cost
as adding or removing a slot, in terms of performance and size costs.
So in general I will try to use it for making existing libraries
safer per-type instead of putting places in the standard library
where it is called dynamically.
Benefits:
- This basically allows us to pass object structures across borders
without having to worry so much about consistency of updates, which
matters most for concurrent applications.
- Future compiler guards for optimization will be much easier if the
VM keeps certain immutability guarantees.
- #cloneSettingSlots:to: as a VM primitive is relatively fast
compared to our normal object-initialization so there should be a
speedup there, although I've only measured it informally and it only
makes a small percentage improvement overall. Still, it helps.
Coming soon:
- A #thaw" and #freeze set of operations for higher-level treatment
of object structures. #freeze would perform a safe recursive deep
copy using #immutably at each level. #thaw would probably not do a
deep reversal of #immutably; a one-level reversal of #immutably would
seem to be enough for most purposes.
- A primitive for quickly setting up an immutable array without
having to make an entirely-redundant mutable array with the same
values just to initialize it.
- An adjustment of the literal objects in Slate, such as compile-time
arrays made via #() and #{} to be immutable, and the same for
Symbols. Other candidates are value-pattern types like Fraction,
Complex, or Point that are created quickly and really are best left
immutable and thrown away rather than modified directly.
The addition of #cloneSettingSlots:to: to the VM resulted in a change
to the VM/image interface, so run "make distclean" and then "make" to
update both image and VM consistently.
Let me know what you think, re: the concept and the idioms.
Enjoy!
--
-Brian
http://tunes.org/~water/brice.vcf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : /archives/slate/attachments/20060301/ad8cb780/PGP.pgp
More information about the Slate
mailing list