Skip to content

perf: replace stream/collector conversions with presized loops - #2

Draft
tobijdc wants to merge 1 commit into
mainfrom
perf/stream-free-conversions
Draft

tobijdc wants to merge 1 commit into
mainfrom
perf/stream-free-conversions

Conversation

@tobijdc

@tobijdc tobijdc commented Oct 19, 2026

Copy link
Copy Markdown
Owner

What

Replaces the stream + custom-Collector conversions with plain presized loops in the four places that still used them:

location path
Structure.convertValue() (list + structure) asObjectMap() / asObject() on nested values
Structure.mapToStructure() objectToValue() for Map payloads
AbstractStructure.asObjectMap() called by providers on every attribute conversion
Value.clone() / Value.objectToValue() (list) ImmutableStructure.getValue()

Each of those allocated a Stream, a lambda, a collector accumulator and an unsized HashMap per call. Presizing via the existing HashMapUtils.forEntries() helper also avoids rehashing.

Why it matters

asObjectMap()/asObject() is how providers hand attributes to vendor backends, and it recurses per nested attribute/list element, so the per-call overhead multiplies. On a local JMH allocation benchmark (-prof gc, conversion-heavy loop calling asObjectMap() + objectToValue() on nested structures) this cut allocated instances from ~1.55M to ~1.24M for the same amount of work (-20%). That benchmark is not part of this PR to keep the diff reviewable — happy to add it as a separate src/test JMH module if you want it in-tree.

Compatibility

  • No signature, visibility or semantic changes. Null handling, deep conversion and the returned map type (HashMap) are unchanged.
  • Iteration order of the presized HashMaps can differ from the previously unsized ones. Map ordering was never part of the contract, but anyone diffing serialized output may notice.

Verification

  • ./mvnw test (JDK 17): 571 unit tests + 8 vmlens tests green, 0 checkstyle violations.

Draft, for review inside the fork only.

The conversion helpers built a stream pipeline, a collector accumulator and an
unsized HashMap on every call, plus boxing in the lambdas:

- Structure.convertValue(): list and structure branches
- Structure.mapToStructure()
- AbstractStructure.asObjectMap()
- Value.clone() (list branch) and Value.objectToValue() (list branch)

These sit on provider-facing paths: asObjectMap()/asObject() is what providers
call to hand attributes to vendor backends, and it recurses per nested value.

Replace them with presized loops using the existing HashMapUtils.forEntries()
helper, which also avoids HashMap rehashing for larger structures.

No signature, visibility or semantic changes; null handling and nested
conversion are unchanged. HashMap iteration order of the presized maps can
differ from the previously unsized ones, but Map ordering was never part of
the contract.
@tobijdc
tobijdc marked this pull request as draft October 19, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant