-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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 jest-worker (up to 4x) #5793
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5793 +/- ##
==========================================
- Coverage 63.74% 63.73% -0.01%
==========================================
Files 216 216
Lines 7929 7938 +9
Branches 4 3 -1
==========================================
+ Hits 5054 5059 +5
- Misses 2874 2878 +4
Partials 1 1
Continue to review full report at Codecov.
|
packages/jest-worker/src/worker.js
Outdated
@@ -49,12 +49,13 @@ export default class { | |||
_busy: boolean; | |||
_child: ChildProcess; | |||
_options: WorkerOptions; | |||
_queue: Array<QueueChildMessage>; | |||
_queue: ?QueueChildMessage; | |||
_last: ?QueueChildMessage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not in the right alphabetical location :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄
This is really fantastic. Thanks @mjesun, great work! |
Beauty!! |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This PR modifies the way internal worker queues are managed, passing from an array to a linked-list, improving a 10% the current benchmark, and 400% over a new benchmark.
How come this wasn't noticed before?
When
jest-worker
was initially benchmarked, it was done by using not so many jobs (~10k), but very heavy in time. While this is the usual approach (few jobs, lots of time per job), sometimes you have the opposite (i.e. lots of jobs, but each of them very fast). This is actually whatjest-haste-map
does.Since the internal queue was an array, re-indexing the queue is a
O(n)
operation, which becomes especially relevant on the second scenario. Switching to a linked list means that all operations for advancing the queue becomeO(1)
, no matter its length. This results in massive speed improvements on really long queues.Some benchmarks
Using the extended performance test, the
empty
function, called 100,000 times results in:Now, with the change, it results in:
What about the existing metrics?
The previous metric (which we initially used for benchmarking,
loadTest
called 10,000 times), has also improved, but only slightly, since that one was already optimized:Tests
I ensured all tests pass, but I also added some slight modifications into the
__performance__tests__
so that you can pass an arbitrary worker method as well as an arbitrary number of iterations. This allowed me to test the other scenario.On a personal note: this is actually WHY algorithms and data structures knowledge IS important for a frontend developer! 🙂