JarrettBillingsley / Croc

Croc is an extensible extension language in the vein of Lua, which also wishes it were a standalone language. Also it's fun.
http://www.croc-lang.org
79 stars 12 forks source link

Change internal representation of instances to make them (considerably) smaller #109

Closed JarrettBillingsley closed 10 years ago

JarrettBillingsley commented 10 years ago

Currently instances have an entire hashtable (or two, if they have hidden fields) embedded inside them. This is not small. On 32-bit platforms, the hashtable header is 20 bytes, and each hash node is 24 bytes. Hashtables have a minimum size of 4 entries. The instance object itself is 24 bytes without the hashtables. This means that an instance with 1-4 regular fields and 1-4 hidden fields will be (24 + 2 * (20 + 4 * 24)) = 256 bytes. That's kind of ridiculous, and you always pay that size cost even if you have a bunch of empty hash slots.

It's doubly ridiculous since the fields never change and we already know the order/structure of the fields from the class. So why bother duplicating that information into every instance?

What I'm thinking is that when a class is frozen, it turns its fields from a regular hash into a pair of things: an array of values (with no "unused" slots), and a lookup hash which maps from names to indices into that array. The instance itself would then store nothing but those values in an array. I'm thinking the instance would then consist of (parent, visitedOnce, pointerToParentTable[, pointerToHiddenFieldLookupTable) + the values themselves. This would make the instance (24 + 12*(number of fields)) bytes. An instance with two fields would be 48 bytes and eight fields would be 120. Much better.

JarrettBillingsley commented 10 years ago

Oh, right -- like arrays, each field/hidden field slot has to have an associated "modified" bool, which would bump the size of each field up to 16 bytes. So a two-field instance would be (24 + 16 * 2) = 56 bytes, and an eight-field instance would be (24 + 16 * 8) = 152 bytes. Still a big improvement.