3 ms·
This structure looked familiar to me: +-------+ | *---+-> hidden class (aka map) +-------+ | *---+-> out-of-object properties storage +-------+
by TazeTSchnitzel 11y ago
This structure looked familiar to me:
+-------+
| *---+-> hidden class (aka map)
+-------+
| *---+-> out-of-object properties storage
+-------+
| *---+-> elements storage
+-------+
| 1 +-+
+-------+ |
| 2 | |
+-------+ > N slots reserved for in-object properties
~~~~~~~~~ |
+-------+ |
| N +-+
+-------+
That's because it's very close to PHP 7's implementation of objects (PHP has "traditional" class-based OOP):
zend_object:
+-------------+-------------+------------+-----------------------------------------+
| gc_info | flags | IS_OBJECT | refcount (32-bit) |
+-------------+-------------+------------+-----------------------------------------+
| UNUSED (32-bit) | handle (32-bit) FIXME |
+----------------------------------------+-----------------------------------------+
| ce - pointer to zend_class_entry (64-bit or 32-bit) |
+----------------------------------------------------------------------------------+
| handlers - pointer to object handlers (64-bit or 32-bit) |
+----------------------------------------------------------------------------------+
| properties - pointer to HashTable of dynamic properties (64-bit or 32-bit) |
+----------------------------------------------------------------------------------+
| guards - pointer to HashTable used for recursion protection (64-bit or 32-bit) |
+----------------------------------------------------------------------------------+
| properties_table[0] - embedded cell of the first declared property (128-bit) |
+----------------------------------------------------------------------------------+
| ... |
+----------------------------------------------------------------------------------+
| properties_table[N] - embedded cell of the last declared property (128-bit) |
+----------------------------------------------------------------------------------+
| optional user data of internal classes (variable size) |
+----------------------------------------------------------------------------------+
63 48 47 40 39 32 31 0
(source: https://wiki.php.net/phpng-int#is_object https://wiki.php.net/phpng-int#is_object)
PHP 7 objects contain N slots for properties defined in the class, plus a hashtable for any additional properties, just like V8 objects.
I guess it's an obvious way to design it.
- mraleph 11y ago> I guess it's an obvious way to design it. It's an obvious way to design it... if you start with explicitly declared properties in your language. For JavaScript a state-of-art for a long time was more like this: +-------+ | *---+-> hashtable for properties +-------+ | | ~~~~~~~~~ various metadata (e.g. prototype chain) ~~~~~~~~~ | | +-------+ all properties mixed together, all stored in a hash table, etc. The non-obvious thing to design here is how you escape from the realm of hashtables into the realm of something both more compact and faster to work with (given certain statistical assumptions about the properties of the code).
- evincarofautumn 11y agoMoreover, if you’re doing the naïve thing, hashtables per se aren’t even particularly good for object properties—most objects are small, so an RB-tree or even linear search performs nicely.