gracelang / minigrace

Self-hosting compiler for the Grace programming language
39 stars 22 forks source link

Accessors that check for initialization could be overwritten for speed #244

Open apblack opened 7 years ago

apblack commented 7 years ago

An accessor method for v compiled for an object must check for an uninitialized variable v before returning (even in a module object, because a method used in the initialization of another field t might access v before it is initialized.)

However, once this check succeeds, the accessor method could be overwritten with a new one that no longer does the check.

Currently, the check persists for ever. This is correct, but changing the accessor might bring a speed increase, particularly if the non-checking accessor is on the prototype, and the checking accessor on the object itself.

apblack commented 4 years ago

Before doing this, it's probably worth checking that it actually yields a speed increase!

Folk wisdom says that methods on a prototype can actually be faster than those on the object. And the check for initialization is likely to be optimized away by the Javascript JIT.

Moreover, having some methods on the object and others on the prototype might inhibit the important optimization of moving all methods to the prototype, except for those that access variables in an enclosing scope.