perf: capture parse Position without boxing the offset Int#876
Open
He-Pin wants to merge 1 commit into
Open
Conversation
This was referenced May 30, 2026
Motivation: Parser.Pos is invoked for nearly every AST node. It was `Index.map(off => new Position(...))`: fastparse's `Index` stores the offset as an Int in its `successValue: Any` field (boxing it), and the `.map` then unboxes it and allocates a closure — per node. boxToInteger via SharedPackageDefs.Index was a top self-frame in the parse flamegraph on kube-prometheus. Modification: - Rewrite Pos to write the Position object straight into successValue via ctx.freshSuccess(new Position(fileScope, ctx.index)), skipping the Int box/unbox and the map closure. Parse output (positions/errors) is unchanged. Result: JMH ParserBenchmark (parse-only, all test-suite files): 1.669 -> 1.579 ms/op (+5.4%, non-overlapping bands). Native parse_time on kube-prometheus: ~105.6 -> ~100.9 ms (+4.5%, consistent). Output byte-identical. 450/450 tests pass.
b8a525f to
c231f70
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.
Motivation
Parser.Posis invoked for nearly every AST node to capture the source offset. It wasIndex.map(off => new Position(...)): fastparse'sIndexstores the offset as anIntin itssuccessValue: Anyfield (boxing it), and the.mapthen unboxes it and allocates a closure — per node.boxToIntegerviaSharedPackageDefs.Indexwas a top self-frame in the parse flamegraph on kube-prometheus.Modification
Positionobject straight intosuccessValueviactx.freshSuccess(new Position(fileScope, ctx.index)), skipping theIntbox/unbox and themapclosure. Parse output (positions / error offsets) is unchanged.Result
Scala Native
--debug-stats parse_timeon kube-prometheus (148 files), interleaved and cooled:→ ~6–8% faster parse; output byte-identical. (JMH
ParserBenchmarkover the test suite showed +5.4% on JVM.)Test plan
./mill __.reformat./mill 'sjsonnet.jvm[3.3.7]'.test— 518/518 pass