如果v8在对象增长时进行反转 [英] If v8 rehashes when an object grows

查看:111
本文介绍了如果v8在对象增长时进行反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你改变了一个对象,这个对象触发了存储散列值的底层数组或数据结构的大小的变化。

  var x = {a:1,b:2,c:3} 
//理论上触发一个调整大小
xd = 4
xe = 5
xf = 6

在v8中,散列的底层数组看起来像这样

  [1,3,2,null,null] 

它最初创造了一些额外的空间。但这还不够,所以它不得不成长。有两种选择。


  1. 它会在原来的位置放置原来的值。
  2. 它增长和 rehashes ,将值移动到任意新的地方。 p>

      //(1)1,3,2停留在
    [1,3,2,6,4 ,5,null,null,null,null]

    //(2)1,3,2被移动
    [6,2,5,3,4,1,null,想知道v8在这种情况下做了什么。也想知道调整大小的启发式是什么(当它超过数量时它是否加倍数组大小)。

V8引擎使用两种对象表示形式: b
$ b


  • 字典模式 - 其中对象作为键值映射存储为散列图

  • 快速模式 - 其中的对象以结构的形式存储,其中在属性访问中不涉及计算。



快速模式对于属性访问通常要快得多 - 但需要知道对象的结构。

V8最初会尝试构建一个对象看起来称为隐藏类的模板。该对象将通过隐藏类转换,直到V8放弃并将该对象存储为缓慢属性。



我深入讨论了这个问题,其中包含
$
$


至于你的直接问题,对象会 快速分配这些属性分配(在每个这样的分配上)并迁移到不同的地图(根据需要复制内存)。


Say you have a change in an object that triggers a change in the size of the underlying array or data structure storing the hash values.

var x = { a: 1, b: 2, c: 3 }
// trigger a resize theoretically
x.d = 4
x.e = 5
x.f = 6

Say the underlying array for the hash looked like this in v8

[ 1, 3, 2, null, null ]

It created some extra space initially. But it wasn't quite enough so then it had to grow. There are two options.

  1. It grows leaving the original values in there current place.
  2. It grows and rehashes, moving the values to arbitrary new places.

So it would look like:

// (1) 1, 3, 2 stay where they are
[ 1, 3, 2, 6, 4, 5, null, null, null, null ]

// (2) 1, 3, 2 are moved
[ 6, 2, 5, 3, 4, 1, null, null, null, null ]

Wondering what v8 does in this situation. Also wondering what the heuristics are for the resizing (does it double the array size when it outgrows, etc.).

解决方案

The V8 engine uses two object representations:

  • Dictionary mode - in which object are stored as key - value maps as a hash map.
  • Fast mode - in which objects are stored like structs, in which there is no computation involved in property access.

Fast mode is typically much faster for property access - but requires the object's structure to be known.

V8 will initially try to construct a template of what the object looks like called a "Hidden Class". The object will transform through hidden classes until V8 will give up and store the object as a slow property.

I go into this more in depth with the relevant code in "How does Bluebird's util.toFastProperties function make an object's properties "fast"? ".

As for your direct question the object would "fast assign" on those property assignments (on each such assignment) and migrate to a different map (copying the memory as needed).

这篇关于如果v8在对象增长时进行反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆