On Thu, Jun 26, 2014 at 12:08 PM, zladuric <[email protected]> wrote:
> What I'd like to add is a little bit of wisdom I've picked up at the
> MLOC-JS.com conference in Budapest this February. Ben Titzer from Google
> Chrome division was explaining a little bit of this, how does GC work in V8,
> and he's shared a great tip:
>
> create as many objects as you can and just discard them.
>
> They're putting a lot of effort into new-gen optimization - it is a lot
> smaller but a lot faster. When your objects get promoted to tenured
> generation space, then it's a lot more work for V8 to manage them + the
> space size itself is bigger, all of which adds a bit overhead.
>
> So what he has said is not to be afraid to just create as many objects as
> you need and just keep discarding that.
>
> I guess that translates to "create as many small local scoped vars as you
> need, and V8 will take care of it all".
That's not bad advice but there's a caveat.
The garbage collector in V8 is generational. It needs to know what
new space objects contain references to old space objects and in order
to get that information, stores to properties in new space objects go
through something called a write barrier[0].
What that means is that an assignment like `object.x = value` gets
translated into code that conceptually looks like this:
// Update write barrier unless value is an int.
if (IsSmallInteger(value) === false) {
RecordWrite(object, 'x');
}
Store(object, 'x', value);
(The write barrier is elided when the value is an integer because
integers are not stored as objects and don't reference other objects.
Numbers can be heap-allocated but signed integers that fit in ~30[1]
bits usually are not.)
You shouldn't let the above worry you too much but in performance
critical code, it sometimes pay not to mix new and tenured objects.
(Something node.js core doesn't do too well, alas. The http and
stream modules, for example, have lots of backlinks between objects
with different lifetimes.)
[0] Not to be confused with a hardware write barrier.
[1] The exact width depends on the hardware architecture.
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/CAHQurc_xmhTG2oQ%2B8%3D0rvNyfqeb5ksrxAhpviEudTAz-bJ3YUA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.