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 1 commit
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 FLAG_ONCE = 1;
benjamingr marked this conversation as resolved.
Show resolved Hide resolved
const FLAG_CAPTURE = 2;
const FLAG_PASSIVE = 4;
const FLAG_NODE_STYLE = 8;
const FLAG_WEAK = 16;
const FLAG_REMOVED = 32;

// 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 |= FLAG_ONCE;
if (capture)
flags |= FLAG_CAPTURE;
if (passive)
flags |= FLAG_PASSIVE;
if (isNodeStyleListener)
flags |= FLAG_NODE_STYLE;
if (Boolean(weak))
flags |= FLAG_WEAK;
this.flags = flags;
Copy link
Contributor

@aduh95 aduh95 Jul 6, 2022

Choose a reason for hiding this comment

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

Suggested change
let flags = 0b0;
if (once)
flags |= FLAG_ONCE;
if (capture)
flags |= FLAG_CAPTURE;
if (passive)
flags |= FLAG_PASSIVE;
if (isNodeStyleListener)
flags |= FLAG_NODE_STYLE;
if (Boolean(weak))
flags |= FLAG_WEAK;
this.flags = flags;
this.flags =
(once && FLAG_ONCE) |
(capture && FLAG_CAPTURE) |
(isNodeStyleListener && FLAG_NODE_STYLE) |
(Boolean(weak) && FLAG_WEAK);

Copy link
Member

@benjamingr benjamingr Jul 6, 2022

Choose a reason for hiding this comment

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

This is a bit too clever for my taste because of the reliance on how && uses the righthand side for the value. (Just personal taste)

Also, why is weak wrapped (before and after) in a conversion?


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 & FLAG_ONCE);
}
get capture() {
return Boolean(this.flags & FLAG_CAPTURE);
}
get passive() {
return Boolean(this.flags & FLAG_PASSIVE);
}
get isNodeEventTarget() {
mabaasit marked this conversation as resolved.
Show resolved Hide resolved
return Boolean(this.flags & FLAG_NODE_STYLE);
}
get weak() {
return Boolean(this.flags & FLAG_WEAK);
}
get removed() {
return Boolean(this.flags & FLAG_REMOVED);
}
set removed(value) {
if (value)
this.flags |= FLAG_REMOVED;
else
this.flags &= ~FLAG_REMOVED;
}

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