Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: use bitset in listener to save memory #43700

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ function weakListeners() {
return { registry: weakListenersState, map: objectToWeakListenerMap };
}

const kFlagOnce = 1 << 0;
const kFlagCapture = 1 << 1;
const kFlagPassive = 1 << 2;
const kFlagNodeStyle = 1 << 3;
const kFlagWeak = 1 << 4;
const kFlagRemoved = 1 << 5;

// The listeners for an EventTarget are maintained as a linked list.
// Unfortunately, the way EventTarget is defined, listeners are accounted
// using the tuple [handler,capture], and even if we don't actually make
Expand All @@ -362,13 +369,21 @@ class Listener {
previous.next = this;
this.previous = previous;
this.listener = listener;
// TODO(benjamingr) these 4 can be 'flags' to save 3 slots
this.once = once;
this.capture = capture;
this.passive = passive;
this.isNodeStyleListener = isNodeStyleListener;

let flags = 0b0;
if (once)
flags |= kFlagOnce;
if (capture)
flags |= kFlagCapture;
if (passive)
flags |= kFlagPassive;
if (isNodeStyleListener)
flags |= kFlagNodeStyle;
if (weak)
flags |= kFlagWeak;
this.flags = flags;
Comment on lines +373 to +384
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really not found of this, I find it harder to read, and my guess is that it's much less performant to reassign the same variable several time rather than once.

Copy link
Member

@Linkgoron Linkgoron Jul 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not sure that I get what the real value of this change is, without some benchmarks at least.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's much less performant to reassign the same variable several time rather than once

Maybe I’m misunderstanding what you’re saying, but V8 can and will optimize this properly. In the machine code V8 currently emits, flags only takes up one register that is repeatedly modified before being stored, which as optimal as it gets. (Interestingly enough, V8 spends a bit more time on determining whether values are false-y, even when the function is only called with boolean arguments. But that’s still an overhead far too small to worry about.)

I'm also not sure that I get what the real value of this change is

Based on the prior TODO comment here, I’d say the expectation is lower memory overhead (one slot in the class instances instead of six different slots). That’s not something you can necessarily observe in a typical benchmark, unfortunately, since ours are geared towards measuring CPU usage, but it’s probably possible to observe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the insight @addaleax, could you share how you inspect what the machine code is emitted by V8?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 there are several good tools for this, the easiest way is probably to run with --log-all and use the syste analyzer https://v8.github.io/tools/head/system-analyzer/index.html

Copy link
Member

@benjamingr benjamingr Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If you really just want to see the bytecode emitted then --print-bytecode would also work, note there are caveats and you can use the v8 module to set flags in runtime to show just the bit you care about)


this.removed = false;
this.weak = Boolean(weak); // Don't retain the object

if (this.weak) {
this.callback = new SafeWeakRef(listener);
Expand All @@ -388,6 +403,31 @@ class Listener {
}
}

get once() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is just a listener and is internal these can probably just be static methods - though I am fine with landing this and iterating :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjamingr Just curious, what would be the advantage of static methods over getters/setters here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I'm not really sure how to articulate the preference. It's something I've seen done with flag properties before but I can't find a justification other than "it'd look more familiar to me based on other code I've seen".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this style because it makes the properties accessible the same way they were before, but yeah, ultimately no strong preferences here from my side, I was just curious.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was honestly convinced I had a good reason before you asked so thanks for asking and making me think about it. Very easy to fall into familiar patterns.

return Boolean(this.flags & kFlagOnce);
}
get capture() {
return Boolean(this.flags & kFlagCapture);
}
get passive() {
return Boolean(this.flags & kFlagPassive);
}
get isNodeStyleListener() {
return Boolean(this.flags & kFlagNodeStyle);
}
get weak() {
return Boolean(this.flags & kFlagWeak);
}
get removed() {
return Boolean(this.flags & kFlagRemoved);
}
set removed(value) {
if (value)
this.flags |= kFlagRemoved;
else
this.flags &= ~kFlagRemoved;
}

same(listener, capture) {
const myListener = this.weak ? this.listener.deref() : this.listener;
return myListener === listener && this.capture === capture;
Expand Down