简体   繁体   English

这个生成的代码应该是什么(打算)做什么的?

[英]What is this generated code supposed (intended) to do?

I saw this in an auto-generated javascript file: 我在自动生成的javascript文件中看到了这个:

function map(x){
    x={x:x};
    delete x.x;
    return x
}

My conclusion is that is used to create an object, but why create it in that way ? 我的结论是用于创建对象,但为什么要以这种方式创建它 Is it a pattern? 这是一种模式吗?

UPDATE UPDATE

More info, the tool that created this code is dart2js from Google, the code is used in this context: 更多信息,创建此代码的工具是来自Google的dart2js,代码用于此上下文:

(function (reflectionData) {
  function map(x){x={x:x};delete x.x;return x}
  if (!init.libraries) init.libraries = [];
  if (!init.mangledNames) init.mangledNames = map();
  if (!init.mangledGlobalNames) init.mangledGlobalNames = map();
  if (!init.statics) init.statics = map();
  if (!init.interfaces) init.interfaces = map();

In the dart source, there's a comment which says that this technique is used for v8 performance reasons: 在dart源中,有一条评论说该技术用于v8性能原因:

// [map] returns an object literal that V8 shouldn't try to optimize with a
// hidden class. This prevents a potential performance problem where V8 tries
// to build a hidden class for an object used as a hashMap.

https://github.com/dart-lang/bleeding_edge/blob/4dde22bc006605fc168cefcc0807c43354463b6e/dart/sdk/lib/_internal/compiler/implementation/js_emitter/reflection_data_parser.dart#L17-L19 https://github.com/dart-lang/bleeding_edge/blob/4dde22bc006605fc168cefcc0807c43354463b6e/dart/sdk/lib/_internal/compiler/implementation/js_emitter/reflection_data_parser.dart#L17-L19

The word map here refers to an associative array 这里的单词map是指关联数组

I read a article about this a while ago actually and apparently if you delete something from a object, V8 puts the object into Dictionary Mode or Slow Mode and then properties are stored in a "hash table" . 我不久前读过一篇关于这个的文章,显然如果你从一个对象中delete一些东西, V8会把对象放到字典模式慢速模式中 ,然后属性存储在“哈希表”中

V8 can handle minor divergences like this just fine, but if your code assigns all sorts of random properties to objects from the same constructor in no particular order, or if you delete properties, V8 will drop the object into dictionary mode, where properties are stored in a hash table. V8可以很好地处理这样的小分歧,但是如果你的代码按照特定顺序为同一个构造函数中的对象分配各种随机属性,或者如果删除属性,V8会将对象放入字典模式,其中存储属性在哈希表中。 This prevents an absurd number of maps from being allocated. 这可以防止分配荒谬的地图数量。

This is the article http://www.jayconrod.com/posts/52/a-tour-of-v8-object-representation it explains it in there along with other things. 这篇文章是http://www.jayconrod.com/posts/52/a-tour-of-v8-object-representation ,它与其他内容一起解释。

I may be wrong but I think this is used for Large (in size and life) objects to increase performance and decrease the chance of a memory leak. 我可能错了,但我认为这用于大型(大小和生命)对象,以提高性能并减少内存泄漏的可能性。

This is on the same sort of topic 这是同一类话题

Does using delete keyword effect v8 optimizations of an object? 是否使用delete关键字对对象进行v8优化?

The purpose of the map function is to create an associative-map object whose set of properties can be quickly altered. map函数的目的是创建一个关联映射对象,其属性集可以快速更改。

The natural question arrises: aren't all JavaScript objects already maps by default? 自然问题是: 默认情况下,并非所有JavaScript对象都已映射过吗? Yes, they are! 对,他们是! The EMCAScript specification allows objects to add or drop properties at any time, allowing them to function as associative maps. EMCAScript规范允许对象随时添加或删除属性,从而允许它们作为关联映射运行。

But, alas, the low-level language that is responsible for implementing the JavaScript execution environment (likely C++) is not so easygoing. 但是,唉,负责实现JavaScript执行环境(可能是C ++)的低级语言并不那么容易。 In particular, V8 uses a concept called hidden classes , whereby the addition of a property to a JavaScript object will cause the creation of a new C++ class. 特别是,V8使用一种称为隐藏类的概念,通过向JavaScript对象添加属性将导致创建新的C ++类。 V8 does this as an optimization because it assumes your code will repeatedly use a small set of object types. V8将此作为优化,因为它假定您的代码将重复使用一小组对象类型。

For example, you have a Bullet type with x , y , dx , and dy properties. 例如,您有一个包含xydxdy属性的Bullet类型。 In practical terms, these types are fixed; 实际上,这些类型是固定的; it's not likely that you would suddenly add on a new property to a Bullet object on the fly. 你不可能在动态时突然将新属性添加到Bullet对象中。 The hidden-class optimization means that using a fixed set of object types runs very quickly, but it also means that, sometimes, the real cost of adding a new property to a JS object can be quite high, because it prompts the creation of a new C++ class that has the new property. 隐藏类优化意味着使用一组固定的对象类型运行得非常快,但这也意味着,有时,向JS对象添加新属性的实际成本可能非常高,因为它会提示创建一个具有新属性的新C ++类。

By introducing a delete operation on the object x , you signal to the V8 engine that this object x will not benefit from the hidden-class optimization. 通过在对象x上引入delete操作,您向V8引擎发出信号,该对象x将无法从隐藏类优化中受益。 The idea behind hidden classes is that your objects will not usually change their set of properties (except adding new properties at construction time). 隐藏类背后的想法是,您的对象通常不会更改其属性集(除了在构造时添加新属性)。 By doing a delete you unequivocally signal that this object will change its property set in ways that make hidden classes totally unhelpful. 通过delete您明确地发出信号,表示此对象将以隐藏类完全无用的方式更改其属性集。 For this object, the cost of creating hidden classes far outweighs the benefits. 对于这个对象,创建隐藏类的成本远远超过了好处。

Thus, the object returned by map will be excluded from V8 hidden-class optimizations, allowing it to add and remove arbitrary properties much more quickly. 因此, map返回的对象将从V8隐藏类优化中排除,允许它更快地添加和删除任意属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM