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 objects are represented as
Val.Objbacked by aLinkedHashMap[String, Val.Obj.Member]. For objects with only a single field (extremely common in array comprehensions and helper functions), the LinkedHashMap overhead is significant:Single-field objects account for a large portion of all objects created during evaluation of typical Jsonnet programs.
Key Design Decision
Store single-field objects inline in
Val.Objitself, using two dedicated fields (singleFieldKey: StringandsingleFieldMember: Val.Obj.Member) instead of allocating a LinkedHashMap. TheLinkedHashMapis only allocated lazily when needed (e.g., object merge+with another object).Fallback to LinkedHashMap is automatic and transparent — no behavioral changes.
Modification
Val.scala: AddedsingleFieldKey/singleFieldMemberfields toVal.Obj, with optimizedvalueRaw/containsKey/visibleKeyNamesfast paths that check inline storage firstsingle_field_object.jsonnetcovering single-field creation, field access, merge, iteration, nested objects, andstd.objectFieldsBenchmark Results
JMH (JVM, 3 iterations)
Hyperfine (Scala Native, 10 runs, vs master)
Analysis
References
he-pin/sjsonnetjit branch commitd284ecf4Result
Consistent -16% JVM improvement for object-heavy workloads by eliminating LinkedHashMap allocation for the most common case (single-field objects).