Skip to content

perf: intern identifier field names + eq fast path in field lookup - #1117

Draft
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/intern-field-eq-fastpath
Draft

perf: intern identifier field names + eq fast path in field lookup#1117
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/intern-field-eq-fastpath

Conversation

@He-Pin

@He-Pin He-Pin commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Motivation

Object field lookup is the hottest path in sjsonnet evaluation. Every field access (containsKey, containsVisibleKey, valueRaw) compares String keys char-by-char via .equals, which is wasteful when the same field names repeat across objects (common in K8s manifests, stdlib).

Modification

  • Parser routes identifier field names through internedStrings, sharing String instances across repeated parses
  • Val.Obj field lookup hot loops use reference equality (eq) before .equals, eliminating char-by-char comparison for interned keys
  • 5 lookup sites patched: containsKey (2), containsVisibleKey (1), valueRaw (2)

Result

Benchmark master this PR Delta
ParserBenchmark.main 1.462 ± 0.053 ms/op 1.378 ± 0.014 ms/op -5.7%
MainBenchmark.main 3.028 ± 0.536 ms/op 3.085 ± 0.529 ms/op +1.9% (noise)

JMH config: -f 2 -wi 5 -i 10 -w 1 -r 1 (2 forks × 10 measurement iterations)

All 424 tests pass. Zero behavioral change — eq is strictly a fast path before the existing .equals fallback.

Test plan

  • ./mill 'sjsonnet.jvm[_]'.test — all pass
  • ./mill bench.runJmh ".*MainBenchmark.*" — no regression
  • ./mill bench.runJmh ".*ParserBenchmark.*" — 5.7% improvement

@He-Pin
He-Pin marked this pull request as draft August 12, 2026 05:35
Motivation:
Object field lookup is the hottest path in sjsonnet evaluation. Every
field access (containsKey, containsVisibleKey, valueRaw) compares
String keys char-by-char via .equals, which is wasteful when the same
field names repeat across objects (common in K8s manifests, stdlib).

Modification:
- Parser routes identifier field names (fieldname rule and Expr.Select)
  through internedStrings, sharing String instances across repeated parses
- String-literal field names are interned in the fieldname rule as well,
  with the same >1024 length guard as constructString to avoid memory
  bloat from pathologically large field names
- Val.Obj field lookup hot loops use reference equality (eq) before
  .equals, eliminating char-by-char comparison for interned keys
- 5 lookup sites patched: containsKey (2), containsVisibleKey (1),
  valueRaw (2)

Result:
ParserBenchmark.main: 1.462 -> 1.378 ms/op (-5.7%); MainBenchmark.main
within noise. All 424 tests pass. Zero behavioral change -- eq is
strictly a fast path before the existing .equals fallback (a eq b
implies a.equals(b)).
@He-Pin
He-Pin force-pushed the perf/intern-field-eq-fastpath branch from b5f7e33 to 2dd7e5f Compare August 13, 2026 11:57
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