perf: single-field object inline storage to avoid LinkedHashMap allocation#687
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
perf: single-field object inline storage to avoid LinkedHashMap allocation#687He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
…ation
For objects with exactly one field (common in patterns like `{ n: X }`),
store the field key and member inline in Val.Obj instead of allocating a
LinkedHashMap. The LinkedHashMap is lazily constructed only when needed
(e.g., key iteration via getAllKeys).
Key changes:
- Val.Obj: added singleFieldKey/singleFieldMember constructor params
- getValue0: lazily constructs LinkedHashMap from inline storage
- valueRaw: single-field fast path with String.equals instead of HashMap.get
- hasKeys/containsKey: fast paths to avoid forcing LinkedHashMap materialization
- visitMemberList: lazy builder allocation, only for 2+ field objects
Upstream: jit branch d284ecf (single-field object avoid LinkedHashMap)
This was referenced Apr 5, 2026
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
Jsonnet frequently creates objects with a single field, e.g.,
{ n: X }in patterns likeFib { n: X }. Currently ALL objects allocate ajava.util.LinkedHashMapto store their fields, which is expensive for single-field objects due to LinkedHashMap's overhead (Node array, load factor, entry objects).This optimization stores the single field key and member inline in
Val.Objusing two simple fields, completely avoiding the LinkedHashMap allocation for the common single-field case.Key Design Decisions
Inline storage: Two new constructor params
singleFieldKey: StringandsingleFieldMember: Obj.Memberstore the field directly when there is exactly one field.Lazy LinkedHashMap construction: When the full map is needed (e.g., key iteration,
addSuper),getValue0lazily constructs a LinkedHashMap from the inline storage. This avoids paying the cost unless actually needed.Lazy builder allocation in
visitMemberList: TheLinkedHashMapbuilder is only allocated when the second field is encountered, not upfront. For single-field objects, noLinkedHashMapis ever allocated during construction.Fast paths for common operations:
valueRaw,containsKey, andhasKeyschecksingleFieldKeydirectly before falling through to the LinkedHashMap path, avoiding materialization.Modification
Val.scalasingleFieldKey/singleFieldMemberconstructor params toVal.ObjgetValue0: Added branch to lazily construct LinkedHashMap from inline storagevalueRaw: Added single-field fast path usingString.equalsinstead ofHashMap.gethasKeys: Returnstrueimmediately for single-field objectscontainsKey: CheckssingleFieldKey.equals(k)for non-super single-field objectscontainsVisibleKey,allKeyNames,visibleKeyNames: Changed from directvalue0togetValue0Evaluator.scalavisitMemberList: Lazy builder allocation — trackssingleKey/singleMember/fieldCountduring field iteration. When exactly 1 field, createsVal.Objwith inline storage. Builder LinkedHashMap is only allocated on the 1→2 field transition.Benchmark Results
JMH (JVM, Scala 3.3.7)
No regressions across all 35 benchmarks.
Hyperfine (Scala Native, macOS ARM64)
Native improvement is modest — the JVM's JIT compiler inlines the
singleFieldKey != nullbranch more aggressively than ahead-of-time compilation.Analysis
The -18.4% improvement on bench.02 is expected: this benchmark (recursive Fibonacci with object inheritance) creates millions of single-field objects like
Fib { n: X }. Each now avoids aLinkedHashMapallocation, and thevalueRawfast path bypasses the HashMap lookup entirely.bench.04 and comparison also benefit because they create objects in tight loops. The optimization is transparent — single-field objects behave identically to multi-field objects, with lazy LinkedHashMap construction on demand.
References
d284ecf4(single-field object avoid LinkedHashMap)Result