Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I saw this in an auto-generated javascript file:

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

More info, the tool that created this code is dart2js from Google, the code is used in this context:

(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();
like image 499
user2070369 Avatar asked Oct 03 '13 18:10

user2070369


People also ask

What is meant by the generated code?

Generated Code means computer code and/or documents which are produced with the Software. Sample 1. Generated Code means an independently executable program generated by the Talend Software.

What are code generators used for?

Code generators are tools that write code for you. It's a super cool way to solve repetitive code problems that can't be solved by writing an abstraction. This may sound like writing code with extra steps, but we will explore all the benefits of using code generators.

What is the purpose of code?

Coding creates a set of instructions for computers to follow. These instructions determine what actions a computer can and cannot take. Coding allows programmers to build programs, such as websites and apps. Computer programmers can also tell computers how to process data in better, faster ways.

What is generated code in Java?

The generated Java code contains an inner abstract class suffixed with ImplBase , such as ServiceNameImplBase . This class defines one Java method for each method in the service definition. It is up to the service implementer to extend this class and implement the functionality of these methods.


3 Answers

In the dart source, there's a comment which says that this technique is used for v8 performance reasons:

// [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

The word map here refers to an associative array

like image 102
SheetJS Avatar answered Oct 19 '22 22:10

SheetJS


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".

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. 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.

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?

like image 45
iConnor Avatar answered Oct 19 '22 21:10

iConnor


The purpose of the map function is to create an associative-map object whose set of properties can be quickly altered.

The natural question arrises: aren't all JavaScript objects already maps by default? Yes, they are! The EMCAScript specification allows objects to add or drop properties at any time, allowing them to function as associative maps.

But, alas, the low-level language that is responsible for implementing the JavaScript execution environment (likely C++) is not so easygoing. 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 does this as an optimization because it assumes your code will repeatedly use a small set of object types.

For example, you have a Bullet type with x, y, dx, and dy properties. 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. 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.

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. 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. 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.

like image 9
apsillers Avatar answered Oct 19 '22 22:10

apsillers