Skip to content

perf: defer Position alloc in exprSuffix2 to the matching branch#877

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/parser-exprsuffix2-defer-pos
Open

perf: defer Position alloc in exprSuffix2 to the matching branch#877
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/parser-exprsuffix2-defer-pos

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented May 30, 2026

Motivation

exprSuffix2 was Pos.flatMapX { i => CharIn(".[({") ... }, which allocated a Position on every attempt — including the failing attempt that terminates exprSuffix2.rep after each expression. Most subexpressions have no suffix, so that trailing failed attempt allocates a Position that is immediately discarded.

Modification

  • Match the suffix char first; allocate new Position(fileScope, ctx.index - 1) only inside the matching branch. No suffix → CharIn fails fast, no Position. Also drops the intermediate .map(_(0)) step. Parse output (positions / error offsets) is unchanged.

Result

  • JMH ParserBenchmark (JVM, over the test suite): 1.560 → 1.530 ms/op (+1.9%, non-overlapping bands).
  • Scala Native --debug-stats parse_time on kube-prometheus (interleaved, cooled): neutral-to-slightly-faster (min 89.1 vs 95.9 ms; mean 107.7 vs 110.0 ms — within noise on this loaded run, no regression).
  • Output byte-identical.

Test plan

  • ./mill __.reformat
  • ./mill 'sjsonnet.jvm[3.3.7]'.test — 518/518 pass

@He-Pin He-Pin marked this pull request as draft May 30, 2026 12:04
Motivation:
exprSuffix2 was `Pos.flatMapX { i => CharIn(".[({")... }`, which allocated a
Position on EVERY attempt — including the failing attempt that terminates
`exprSuffix2.rep` after each expression. Most subexpressions have no suffix, so
that trailing failed attempt (one per expression) allocated a Position that was
immediately discarded.

Modification:
- Match the suffix char first; allocate `new Position(fileScope, ctx.index - 1)`
  only inside the matching branch. No suffix -> CharIn fails fast, no Position.
  Also drops the `.map(_(0))` Char step. Parse output (positions/errors) is
  unchanged.

Result:
JMH ParserBenchmark (-f0, same-session): 1.560 -> 1.530 ms/op (+1.9%). Native
parse_time on kube-prometheus: non-regressing, min/p25 ~2% lower (noise-limited
on a loaded machine). Output byte-identical. 517/517 tests pass.
@He-Pin He-Pin force-pushed the perf/parser-exprsuffix2-defer-pos branch from 061c6ce to 572d45c Compare May 30, 2026 12:42
@He-Pin He-Pin marked this pull request as ready for review May 30, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant