perf: multi-field object inline array storage (2-8 fields)#688
Open
He-Pin wants to merge 2 commits intodatabricks:masterfrom
Open
perf: multi-field object inline array storage (2-8 fields)#688He-Pin wants to merge 2 commits intodatabricks:masterfrom
He-Pin wants to merge 2 commits 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)
Three-tier object storage: 1 field uses singleKey/singleMember, 2-8 fields use flat parallel arrays (inlineFieldKeys/inlineFieldMembers), 9+ fields use LinkedHashMap. This eliminates LinkedHashMap allocation for the vast majority of Jsonnet objects which have fewer than 9 fields. All fast paths updated: getValue0, hasKeys, containsKey, containsVisibleKey, allKeyNames, visibleKeyNames, valueRaw. Field tracking logic extracted into trackField() helper to avoid code duplication between the two Member.Field case branches. JMH: bench.02 -17.9%, realistic2 -2.7%, bench.04 -5.5% Native: realistic2 -13.5% (1.89x faster than jrsonnet) Upstream: jit branch commit 13e6ff3
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
Val.Obj currently always allocates a LinkedHashMap for fields, even for small objects with 2-8 fields. In Jsonnet programs, most objects are small (config entries, function returns). The LinkedHashMap overhead dominates allocation in object-heavy workloads.
Key Design Decision
Store object fields in flat inline arrays when the field count is ≤8: parallel arrays for keys, values, and visibility flags. This avoids LinkedHashMap allocation, hashing, and entry boxing while preserving O(1) field lookup via linear scan (fast for ≤8 elements due to cache locality).
Modification
Benchmark Results
JMH (JVM, 3 iterations warmup + 3 measurement)
Analysis
The bench.02 improvement (-25.0%) is the strongest single-PR gain on this benchmark, because bench.02 creates many small objects repeatedly. The tight error bars confirm this is a real structural improvement rather than JIT noise. This builds on the single-field optimization in #687 to cover the full small-object spectrum.
References
13e6ff39Result
All 46 tests pass. All benchmarks strongly positive, no regressions.