On Thu, Jun 12, 2014 at 12:59 PM, Ben Noordhuis <[email protected]> wrote:
> On Thu, Jun 12, 2014 at 12:18 PM, Ilya Kantor <[email protected]> wrote: > > Do I get it right that unoptimized code is shared but optimize code is > not > > (or shared partially or...)? > As I said, in some cases it is shared, but in others it is not (the details are technical and might change). > > > > If closure is the root of all evil, will it help with sharing if I remove > > the closure completely? > > Not quite. What I think Jacob tried to convey is that a closure > basically looks like this: > > struct Closure { > Function* function; > Context* context; > }; Right. "Closure" is a bit of an overloaded term. I meant function objects (or "instances" if you prefer). Even if they don't access variables from their outer scope, by executing "foo = function() {...}" every time the constructor runs, you'll get a new function object (i.e. a new "closure") every time. > Each closure has its own context object but they all start out with > the same non-optimized function. > > If you have three identical closures and call one of them a million > times, V8 will eventually optimize it and update the closure object to > make it point to the new, optimized function. The other two will > still be pointing to the non-optimized version at that point. > > If you then start calling the second closure frequently, the optimizer > is going to discover that it already has an optimized function for > this kind of closure and update the closure object accordingly. > > > Like this: > > ``` > > function Machine(power) { > > > > this.enable = function() { > > this._enabled = true; > > }; > > this.disable = function() { > > this._enabled = false; > > }; > > } > > > > var machines = [] > > for(var i=0; i<10000; i++) machines.push(new Machine) > > ``` > > I believe you should unconditionally set `this._enabled = false` (or > true) in the constructor to prevent unnecessary hidden class > transitions. > Definitely! > I'm not 100% sure if it matters in this particular example so someone > please correct me if I'm wrong. > Whether it matters for performance in any given case can only be determined by measuring (and depends on surrounding code), but it's a very good rule of thumb. -- -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
