perf(core): eliminate allocations in emit hot path#185
Merged
Conversation
- Reuse a module-level Date object instead of allocating new Date() on every emit/addLog call (isoNow helper) - Skip TailSamplingContext object allocation when no tail sampling is configured (the common case) - Avoid empty object allocation when emit() is called without overrides
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
Hey there and thank you for opening this pull request! 👋🏼 We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted. Details: |
Contributor
Benchmark reportBundle size
|
commit: |
Merging this PR will degrade performance by 50.38%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | emit minimal event |
132.5 µs | 95.6 µs | +38.6% |
| ❌ | duration match |
25.2 µs | 44.6 µs | -43.41% |
| ⚡ | evlog |
149.1 µs | 115.2 µs | +29.41% |
| ❌ | path glob match |
25.4 µs | 44.6 µs | -42.95% |
| ❌ | status match |
25.4 µs | 51.1 µs | -50.38% |
| ❌ | winston |
452.6 µs | 565.5 µs | -19.96% |
| ❌ | no match (fast path) |
25.6 µs | 45.4 µs | -43.67% |
| ⚡ | full emit with sampling (likely sampled out) |
72 µs | 61.3 µs | +17.48% |
| ⚡ | emit + JSON.stringify |
144.5 µs | 107 µs | +35.05% |
| ❌ | winston — 4 separate log calls |
431.6 µs | 592.1 µs | -27.11% |
| ❌ | push 100 events (no flush) |
424.2 µs | 563.9 µs | -24.78% |
| ❌ | push 1000 events (no flush) |
1.8 ms | 2.6 ms | -27.75% |
| ⚡ | Firefox Linux |
219.3 µs | 87.5 µs | ×2.5 |
| ⚡ | with traceparent |
472.7 µs | 337.6 µs | +40.02% |
Comparing perf/emit-hot-path (2f8061b) with main (609ad97)1
Footnotes
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.
Summary
Dateobject viaisoNow()instead ofnew Date().toISOString()on every emit — eliminates 1 heap allocation per call (pattern used by pino)TailSamplingContextobject allocation when no tail sampling is configured (the default/common case) — saves 1 object + 5 property assignments per emitif (overrides)instead of(overrides ?? {})— avoids allocating an empty object when no overrides are passedThese target
emitWideEvent(45% CPU in CodSpeed profiling) andemit()directly.