X Tutup
The Wayback Machine - https://web.archive.org/web/20240926120243/https://github.com/nodejs/node/pull/34120
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

Improve EventTarget performance #34120

Closed

Conversation

Ethan-Arrowood
Copy link
Contributor

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Work in progress PR for improving performance of EventTarget by optimizing for singular handlers and then utilizing an array rather than a linked list.

Closes #34074

@Ethan-Arrowood
Copy link
Contributor Author

Ethan-Arrowood commented Jun 29, 2020

Benchmark: ./node benchmark/events/eventtarget.js

Before my changes:

events/eventtarget.js listeners=1 n=1000000: 609,398.4958350995
events/eventtarget.js listeners=5 n=1000000: 612,860.7867785434
events/eventtarget.js listeners=10 n=1000000: 605,421.3805186535

After my changes

events/eventtarget.js listeners=1 n=1000000: 697,612.8094972048
events/eventtarget.js listeners=5 n=1000000: 678,713.3416166586
events/eventtarget.js listeners=10 n=1000000: 624,031.739092949

😅 I think I have to make some changes

@Ethan-Arrowood
Copy link
Contributor Author

The latest commit has some decent perf improvements:

events/eventtarget.js listeners=1 n=1000000: 471,435.62430082005
events/eventtarget.js listeners=5 n=1000000: 429,752.2592840155
events/eventtarget.js listeners=10 n=1000000: 489,863.1655030646

Based on @ronag comment I ditched the Listener class and the singular listener optimization and used an array of arrays (each inner array is a listener). I'm going to try replacing the inner array with an object next

@Ethan-Arrowood
Copy link
Contributor Author

I tried using object inside the array and while it makes the code clearer it is about the same speed as the original code.

I think the current improvement is going to be the best improvement for now

@targos
Copy link
Member

targos commented Jun 29, 2020

@Ethan-Arrowood The numbers printed in the benchmark logs are in operations / second, so when you compare with a previous version, higher is better.

@Ethan-Arrowood
Copy link
Contributor Author

higher is better.

😂 oh whoops! Okay hmm so the array version is not better, and my first attempt was only marginally better 🤔

This reverts commit 89ba8d4.
@Ethan-Arrowood
Copy link
Contributor Author

I'm going to leave this open so others can comment, but I do believe focussing on spec compliance is better right now. Will open a new issue and begin work on that shortly.

@jasnell
Copy link
Member

jasnell commented Jun 30, 2020

To be honest, I really wouldn't expect the array version to be faster than the linked list here, and the linked list is already optimized for the single listener case. There are definitely performance improvements that can be made but I don't think this is it.

@Ethan-Arrowood
Copy link
Contributor Author

Yeah I agree! Do you think I should close this PR? I already have another branch focussing on adding the WPT stuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Performance of EventTarget
4 participants
X Tutup