lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Jun 1, 2013, at 11:02 AM, David Favro <lua@meta-dynamic.com> wrote:

Ben Jackson implemented a bunch of optimizations to recognize this situation and mutate lst in place if it's the last reference. Note that in loop bodies, if you didn't have the last reference initially, you will have the last reference after the first iteration's copy.

This comes up often when creating "Copy-On-Write" implementations of objects that want mutable semantics but also the ability to efficiently make and pass around copies that may never get modified, e.g. string classes in C++.

Trouble is, both shallow clone and copy-on-write are awkward to implement in a GC model. An optimized shallow clone that didn't do the clone if there is only one reference essentially implies a garbage collect (read: expensive) in order to determine the reachability of the table.

Copy-on-write implies a level of indirection, whereby the "real" table is accessed indirectly by referenced intermediaries, as it is necessary to keep track on which variables reference which (phantom) copy of the table, so that when a write finally occurs, the two sets of references can be split appropriately. And actually, if you are careful, you can implement this in Lua using metatables.

--Tim