perf: add tinybench benchmarks, CodSpeed CI, and reduce tap-registration allocations#217
Open
alexander-akait wants to merge 4 commits intomainfrom
Open
perf: add tinybench benchmarks, CodSpeed CI, and reduce tap-registration allocations#217alexander-akait wants to merge 4 commits intomainfrom
alexander-akait wants to merge 4 commits intomainfrom
Conversation
…ion allocations
- Add tinybench-based micro-benchmarks covering SyncHook, SyncBailHook,
SyncWaterfallHook, SyncLoopHook, AsyncSeries*, AsyncParallel*, HookMap
and the interceptor paths, plus tap registration and first-call compile.
- Wire up @codspeed/tinybench-plugin so the same files double as CodSpeed
benchmarks in CI and print a tinybench table locally.
- Add a CodSpeed GitHub Actions workflow (.github/workflows/codspeed.yml)
that runs on push to main and on pull requests.
- Perf: Hook#_tap builds the final tap descriptor in a single allocation
for the common string-options case (hook.tap("name", fn)), making
tap registration ~2x faster in micro-benchmarks.
- Perf: HookCodeFactory#setup uses a preallocated array + explicit loop
instead of Array.prototype.map.
|
🦋 Changeset detectedLatest commit: f72b1fd The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #217 +/- ##
==========================================
- Coverage 97.96% 97.94% -0.03%
==========================================
Files 13 13
Lines 689 730 +41
Branches 112 121 +9
==========================================
+ Hits 675 715 +40
- Misses 13 14 +1
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Restructure benchmarks/ into per-category directories (sync/, async/,
hookmap/, interceptors/, registration/) with one bench file per hook
type, add a recursive runner (benchmarks/run.js) that shares the
CodSpeed session across suites, and add new benchmarks:
- sync: variants for tap counts 0/1/3/5/10/20/50, arg counts 0..5,
bail positions, waterfall returning vs. undefined, loop with
0/N reloops, args-reading tap.
- async: sync/async/promise tap variants for Series, SeriesBail,
SeriesWaterfall, Parallel, ParallelBail; new AsyncSeriesLoopHook
benchmark; .promise() flavor coverage.
- hookmap: hot get/for, missing key, cold factory with 0/1/3
interceptors; new MultiHook benchmarks (tap, isUsed, intercept).
- interceptors: baseline vs. call/tap/register/combined/multiple
for SyncHook and AsyncSeries/AsyncParallel; late-intercept
re-compile case.
- registration: tap/tapAsync/tapPromise with string/object/stage/
before options; per-hook-type first-call compile cost.
The runner supports an optional subdir arg (node benchmarks/run.js sync),
and individual files remain runnable stand-alone via the runIfMain
shim in helpers.js. npm scripts bench:{sync,async,hookmap,interceptors,
registration} target each category.
…line HookMap#for lookup - Hook#_insert: add an O(1) fast path for the overwhelmingly common append case (no before, consistent stage). Previously the shift loop always ran at least once, performing one unnecessary write per tap. - Hook#_runRegisterInterceptors: early-return when there are no interceptors and use an indexed loop instead of for...of to avoid iterator allocation. - HookMap#for: inline the _map.get() lookup instead of delegating through this.get(key); this is hit on every hook access in consumers like webpack, so saving a method dispatch is worth it. Impact (local micro-benchmarks): - SyncHook#tap (10 taps, string options): ~482 ns -> ~397 ns (~18%). - AsyncSeriesHook#tapAsync / tapPromise (10 taps): ~480 ns -> ~400 ns. - SyncHook: tap 5 + first call (compile): ~4350 ns -> ~4150 ns (~5%). - HookMap#for (existing key): ~80 ns -> ~76 ns (~6%). - .call() paths are unchanged. All 98 existing tests still pass.
Compile-path (HookCodeFactory) wins: - init: use Array.slice() instead of spread to skip the iterator protocol, and reset a new _joinedArgs cache. - args: memoize the common no-before/no-after result so arguments are joined once per compile rather than once per tap. - needContext: indexed loop instead of for...of. - callTapsSeries: inline findIndex, cache taps and tapsLength, hoist the tap-invariant doneBreak closure out of the compile-time loop. - callTapsParallel: cache taps / tapsLength, hoist both done and doneBreak closures out of the loop - they don't depend on i. MultiHook: indexed loops with cached `this.hooks`. Result: SyncHook tap-5+first-call (compile) dropped from ~4150ns to ~3700ns (~11% faster) and similar improvements across other hook types. The .call() path is unchanged. All 98 tests still pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SyncWaterfallHook, SyncLoopHook, AsyncSeries*, AsyncParallel*, HookMap
and the interceptor paths, plus tap registration and first-call compile.
benchmarks in CI and print a tinybench table locally.
that runs on push to main and on pull requests.
for the common string-options case (hook.tap("name", fn)), making
tap registration ~2x faster in micro-benchmarks.
instead of Array.prototype.map.