为什么convertToFastObject函数使它快? [英] Why the convertToFastObject function make it fast?

查看:127
本文介绍了为什么convertToFastObject函数使它快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在1.0版本后尝试了Dart SDK,并在Dart中写了一个简单的hello-world程序。
然后,使用SDK工具,我生成了JavaScript文件:helloworld.dart.js
我通过输出js代码,我看到有一个名为convertToFastObject的函数。
定义是:

I tried Dart SDK after the 1.0 release, and wrote a simple hello-world program in Dart. Then, with the SDK tool, I generated the JavaScript file: helloworld.dart.js I went through the output js code, I saw there is a function named convertToFastObject. The definition is:

function convertToFastObject(properties) {
    function MyClass() {};
    MyClass.prototype = properties;
    new MyClass();
    return properties;
}

使用代码如下:

A = convertToFastObject(A);
B = convertToFastObject(B);

我知道此代码适用于各种浏览器,而不适用于Chromium / Chrome。
我不明白,为什么函数可以使对象更快?

I know this code is for various kinds of Browsers, not for Chromium/Chrome only. I cannot understand, why the function can make the Object faster?

推荐答案

引擎。

可以肯定的是,这段代码片段看起来很奇怪:它分配属性 MyClass ,然后使用构造函数构建一个 new MyClass()的实例,然后返回 properties 。这是奇怪的,因为1)属性从未改变,2)函数从不使用 MyClass

To be sure, this code snippet looks pretty weird: it assigns properties as the prototype of a constructor MyClass, then uses the constructor to build an instance with new MyClass(), and then returns properties. This is strange because 1) properties is never altered, and 2) the function never uses MyClass or the instance ever again.

每当你看到这样奇怪的行为时,你可以相当肯定这是一个速度优化。在这种情况下,通过使用V8的隐藏类优化获得速度。从 Dart来源的紧密相关部分

Whenever you see strange behaviors like this, you can be fairly sure it's a speed optimization. In this case, the speed is gained by using V8's "hidden class" optimization. From a closely-related section of the Dart source:

// Use the newly created object as prototype. In Chrome,
// this creates a hidden class for the object and makes
// sure it is fast to access.

在V8引擎中,构造的对象被赋予一个隐藏C ++类来表示它的集合属性。通过构造一个其原型是 properties 对象的对象, properties 的属性值成为新实例的C ++隐藏类,这提高了属性访问速度。

In the V8 engine, a constructed object is given a "hidden" C++ class to represent its set of properties. By constructing an object whose prototype is the properties object, the property values of properties become part of the new instance's C++ hidden class, which improves property-access speed.

我相信V8中的所有对象都默认隐藏类,所以这种技术的需要不是立即显而易见。但是,对象可能会丢失隐藏的类(并输入慢模式或字典模式),通过证明它不受益于优化。当一个对象 delete 的一个属性或者添加太多与任何其他对象的属性无关的属性时,V8假定一个共享的隐藏类是无价值的,因为对象没有其他类似的对象来共享它的隐藏类。这个 convertToFastObject 函数可以通过使用它作为新构造的实例的原型,将慢模式对象的权利重新赋予隐藏类。

I believe all objects in V8 have hidden classes by default, so the need for this technique isn't immediately obvious. However, it is possible for an object to lose its hidden class (and enter "slow mode" or "dictionary mode") by demonstrating that it doesn't benefit from the optimization. When an object deletes one of its properties or adds too many properties that are unrelated to the properties of any other objects, V8 assumes that a shared hidden class isn't valuable, because the object has no other similar object to share its hidden class with. This convertToFastObject function can re-instate a "slow mode" object's right to a hidden class by using it as the prototype of a newly constructed instance.

相关的隐藏类问题,来自不同的Dart优化:这个生成的代码是什么(打算) ?

Related hidden class question, arising from a different Dart optimization: What is this generated code supposed (intended) to do?

这篇关于为什么convertToFastObject函数使它快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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