Comment #105 on issue 164 by [email protected]: Wrong order in Object properties interation
http://code.google.com/p/v8/issues/detail?id=164

The V8 devs are reading this, and discussing it. You have almost convinced me, for example. The problem is that JSObjects and JSArrays are implemented with the same code, and changing the behavior of one of them means that every access to any property of an object or array would need an additional check to see if the object
is a JSObject or JSArray.

Changing both to keep the insertion order would increase the size of all large JSArray objects by a factor of 3, and plenty of application code relies on using
lots of large arrays.

A solution that might have minimal impact is based on the fact that elements with numeric indices are stored in one of two ways. A dense set of elements, like elements with keys 1,2,3,4,5,6,7,8,9, and 10, are stored as "fast elements", which take one word per element, and have optimized fast methods. A non-dense set of elements, like elements 2415436, 5403932, and 3060393, are stored as "slow elements", a hash table which includes an insertion order field for each element. The fact that this insertion order is maintained, for "slow elements", is because V8 does maintain the insertion order for almost all object properties, just not those with numeric indices. This is done for compatibility reasons - it just does not go the final step
to include properties with numeric indices.

If JSObjects only used "slow elements", and the for-in loop used insertion order for
JSObject slow elements, and sorted order for JSArray slow elements (and fast
elements), then JSObjects could maintain insertion order, without much slowdown. The only disadvantage would be if people were using JSObjects like arrays. A
JSObject with elements 1 through 1000 would now be much slower and bigger.

But, again, it is not just synthetic benchmarks that depend on large arrays, so we cannot make all arrays in the system 3 times their current size. V8 implements JSArray and JSObject the same way, and making Arrays and Objects behave differently is hard to do without slowing down everything in the system, because every use of something would need to check if it is an array or object. If this change was easy
to make, it would have been done already.


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to