perf: intern identifier field names for faster field lookup - #1117
Open
He-Pin wants to merge 1 commit into
Open
Conversation
2 tasks
He-Pin
marked this pull request as draft
August 12, 2026 05:35
He-Pin
force-pushed
the
perf/intern-field-eq-fastpath
branch
from
August 13, 2026 11:57
b5f7e33 to
2dd7e5f
Compare
He-Pin
force-pushed
the
perf/intern-field-eq-fastpath
branch
from
August 20, 2026 12:12
2dd7e5f to
1c34dfb
Compare
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 - With interned keys, String.equals hits its built-in reference check on the first line, so lookups short-circuit without char-by-char comparison Result: ParserBenchmark.main: 1.462 -> 1.378 ms/op (-5.7%); MainBenchmark.main within noise. All tests pass. Zero behavioral change -- interning only affects String identity, never equality semantics.
He-Pin
force-pushed
the
perf/intern-field-eq-fastpath
branch
from
August 22, 2026 09:33
b2383d5 to
0caf987
Compare
He-Pin
added a commit
to He-Pin/sjsonnet
that referenced
this pull request
Aug 22, 2026
Motivation: Object keys are rendered (quoted + escaped) on every materialization. In K8s manifests and similar workloads, the same short keys repeat thousands of times. Each render re-runs getChars + escape scan. Modification: - 64-slot identity-keyed cache for quoted object key bytes - Two-touch pattern: store key on first sight, capture bytes on second identity hit (avoids byte[] allocation for one-shot keys) - Cache hit: System.arraycopy pre-rendered bytes (zero escape scan) - Keys > 32 chars bypass cache entirely (rare, not worth caching) - Identity-keyed (eq) so no hashCode/equals overhead on lookup - Lazy initialization avoids allocation for small outputs Result: MainBenchmark: within noise (no regression). The cache synergizes with identifier interning (databricks#1117) which guarantees stable String identity for field names, maximizing hit rate.
He-Pin
marked this pull request as ready for review
August 22, 2026 09:33
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
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
fieldname,Select) throughinternedStrings, sharing String instances across repeated parses (same> 1024length guard as the existing string-literal interning).String.equalshits its built-in reference check on the first line, so lookups short-circuit without char-by-char comparison.An earlier revision also prefixed the lookup loops with an explicit
(x eq k) ||fast path; it was dropped becauseString.equalsalready begins with an identity check on JVM and Scala Native, and on Scala.js botheqandequalscompile to===, so the prefix double-compared every non-matching key.Result
JMH config:
-f 2 -wi 5 -i 10 -w 1 -r 1(2 forks × 10 measurement iterations)All tests pass. Zero behavioral change — interning only affects String identity, never equality semantics.
Test plan
./mill 'sjsonnet.jvm[_]'.test— all pass./mill bench.runJmh ".*MainBenchmark.*"— no regression./mill bench.runJmh ".*ParserBenchmark.*"— 5.7% improvement