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
Most Jsonnet objects have fewer than 9 fields. Currently, every object (except single-field objects from PR #687) allocates a
LinkedHashMapto store its field names and members. This is wasteful for small objects where a flat array lookup is both faster and more memory-efficient.Key Design Decision
Extend the existing single-field optimization (#687) with a three-tier storage strategy:
singleFieldKey/singleFieldMemberinlineFieldKeys[]/inlineFieldMembers[]LinkedHashMap(existing)The threshold of 8 was chosen because:
Modification
Val.scala
inlineFieldKeys: Array[String]andinlineFieldMembers: Array[Val.Obj.Member]toVal.ObjconstructorgetValue0,hasKeys,containsKey,containsVisibleKey,allKeyNames,visibleKeyNames,valueRaw.clone()to prevent caller mutation of internal stateLinkedHashMapconstruction ingetValue0when needed (e.g., foraddSuper)Evaluator.scala
trackField()helper for singleKey → inlineKeys → builder transitionsgetEmptyValueCacheForObjWithoutSuper(fieldCount)to use actual field count (notfields.length) — fixes over-allocation when computed field names resolve to nullBenchmark Results
JMH (JVM, single iteration, ms/op — lower is better)
All 35 benchmarks: zero regressions.
Native (Scala Native, hyperfine, ms — lower is better)
Analysis
The multi-field optimization primarily benefits workloads that create many small objects:
Native shows less benefit on bench.02 than JVM, suggesting the JVM JIT can better exploit the simplified object layout for inlining. The realistic2 native improvement (-13.4%) confirms the optimization helps real-world workloads.
References
13e6ff39Result
All 140 tests pass. Zero benchmark regressions across 35 benchmarks. Significant improvements on object-heavy workloads, especially realistic2 where sjsonnet is now 1.84x faster than jrsonnet on native.