perf(concat): hoist per-token chunk state out of the per-iteration closure#331
Open
Boshen wants to merge 1 commit into
Open
perf(concat): hoist per-token chunk state out of the per-iteration closure#331Boshen wants to merge 1 commit into
Boshen wants to merge 1 commit into
Conversation
Merging this PR will improve performance by 2.5%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | add_sourcemap_loop |
363 µs | 353 µs | +2.83% |
| ⚡ | from_sourcemaps |
344.3 µs | 334.3 µs | +3.02% |
| ⚡ | lookup_table[real_medium] |
1.5 µs | 1.5 µs | +1.97% |
| ⚡ | lookup_table[real_small] |
1.4 µs | 1.3 µs | +2.18% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing perf/concat-hoist-state (15efed1) with main (9323530)
Footnotes
-
5 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
…osure `add_sourcemap`'s per-token loop captured `&mut self` inside the `get_source_id().map(...)` / `get_name_id().map(...)` closures so it could update `token_chunk_prev_source_id` / `token_chunk_prev_name_id` on every iteration. That forced the compiler to round-trip the prev_* fields through memory (load, mutate, store via the `self` pointer) for every token rather than keeping them in registers. Track the running prev values in plain locals, then write them back to `self` once at the end. Also handle the first token separately so the `i == 0` dedup check no longer runs every iteration. Behavior, including the duplicate-token de-duplication at the concatenation seam, is unchanged.
10ebd62 to
15efed1
Compare
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
`add_sourcemap`'s per-token loop captured `&mut self` inside the `get_source_id().map(...)` / `get_name_id().map(...)` closures so it could update `token_chunk_prev_source_id` / `token_chunk_prev_name_id` on every iteration. That forced the compiler to round-trip the `prev_*` fields through memory (load, mutate, store via the `self` pointer) for every token rather than keeping them in registers.
Track the running prev values in plain locals, then write them back to `self` once at the end. Also handle the first token separately so the `i == 0` dedup check no longer runs every iteration.
Behavior, including the duplicate-token de-duplication at the concatenation seam, is unchanged.