From ff0c50494c5a5a75be2fc85253d4ee5106e82c9f Mon Sep 17 00:00:00 2001 From: Tobias Ibounig Date: Mon, 19 Oct 2026 21:18:20 +0200 Subject: [PATCH] perf: replace stream/collector conversions with presized loops 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. --- .../openfeature/sdk/AbstractStructure.java | 13 ++++--- .../java/dev/openfeature/sdk/Structure.java | 34 +++++++++++-------- src/main/java/dev/openfeature/sdk/Value.java | 16 ++++++--- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/main/java/dev/openfeature/sdk/AbstractStructure.java b/src/main/java/dev/openfeature/sdk/AbstractStructure.java index 4ab4f7f12..bbed24269 100644 --- a/src/main/java/dev/openfeature/sdk/AbstractStructure.java +++ b/src/main/java/dev/openfeature/sdk/AbstractStructure.java @@ -3,6 +3,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; @SuppressWarnings({"PMD.BeanMembersShouldSerialize", "checkstyle:MissingJavadocType"}) @@ -42,13 +43,11 @@ public Map asUnmodifiableMap() { */ @Override public Map asObjectMap() { - return attributes.entrySet().stream() - // custom collector, workaround for Collectors.toMap in JDK8 - // https://bugs.openjdk.org/browse/JDK-8148463 - .collect( - HashMap::new, - (accumulated, entry) -> accumulated.put(entry.getKey(), convertValue(entry.getValue())), - HashMap::putAll); + Map objectMap = HashMapUtils.forEntries(attributes.size()); + for (Entry entry : attributes.entrySet()) { + objectMap.put(entry.getKey(), convertValue(entry.getValue())); + } + return objectMap; } @Override diff --git a/src/main/java/dev/openfeature/sdk/Structure.java b/src/main/java/dev/openfeature/sdk/Structure.java index 4a52580a8..43644286c 100644 --- a/src/main/java/dev/openfeature/sdk/Structure.java +++ b/src/main/java/dev/openfeature/sdk/Structure.java @@ -3,10 +3,11 @@ import static dev.openfeature.sdk.Value.objectToValue; import dev.openfeature.sdk.exceptions.ValueNotConvertableError; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; -import java.util.stream.Collectors; /** * {@link Structure} represents a potentially nested object type which is used to represent @@ -94,16 +95,21 @@ default Object convertValue(Value value) { } if (value.isList()) { - return value.asList().stream().map(this::convertValue).collect(Collectors.toList()); + List listValue = value.asList(); + List converted = new ArrayList<>(listValue.size()); + for (Value element : listValue) { + converted.add(convertValue(element)); + } + return converted; } if (value.isStructure()) { - Structure s = value.asStructure(); - return s.asUnmodifiableMap().entrySet().stream() - .collect( - HashMap::new, - (accumulated, entry) -> accumulated.put(entry.getKey(), convertValue(entry.getValue())), - HashMap::putAll); + Map structureAttributes = value.asStructure().asUnmodifiableMap(); + Map converted = HashMapUtils.forEntries(structureAttributes.size()); + for (Entry entry : structureAttributes.entrySet()) { + converted.put(entry.getKey(), convertValue(entry.getValue())); + } + return converted; } throw new ValueNotConvertableError(); @@ -116,10 +122,10 @@ default Object convertValue(Value value) { * @return a Structure object in the SDK format */ static Structure mapToStructure(Map map) { - return new MutableStructure(map.entrySet().stream() - .collect( - HashMap::new, - (accumulated, entry) -> accumulated.put(entry.getKey(), objectToValue(entry.getValue())), - HashMap::putAll)); + Map attributes = HashMapUtils.forEntries(map.size()); + for (Entry entry : map.entrySet()) { + attributes.put(entry.getKey(), objectToValue(entry.getValue())); + } + return new MutableStructure(attributes); } } diff --git a/src/main/java/dev/openfeature/sdk/Value.java b/src/main/java/dev/openfeature/sdk/Value.java index c13094d98..b69e2cf67 100644 --- a/src/main/java/dev/openfeature/sdk/Value.java +++ b/src/main/java/dev/openfeature/sdk/Value.java @@ -4,9 +4,9 @@ import dev.openfeature.sdk.exceptions.TypeMismatchError; import java.time.Instant; +import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import lombok.EqualsAndHashCode; import lombok.SneakyThrows; import lombok.ToString; @@ -288,7 +288,11 @@ public Instant asInstant() { @Override protected Value clone() { if (this.isList()) { - List copy = this.asList().stream().map(Value::new).collect(Collectors.toList()); + List listValue = this.asList(); + List copy = new ArrayList<>(listValue.size()); + for (Value element : listValue) { + copy.add(new Value(element)); + } return new Value(copy); } if (this.isStructure()) { @@ -325,8 +329,12 @@ public static Value objectToValue(Object object) { } else if (object instanceof Structure) { return new Value((Structure) object); } else if (object instanceof List) { - return new Value( - ((List) object).stream().map(o -> objectToValue(o)).collect(Collectors.toList())); + List listValue = (List) object; + List values = new ArrayList<>(listValue.size()); + for (Object element : listValue) { + values.add(objectToValue(element)); + } + return new Value(values); } else if (object instanceof Instant) { return new Value((Instant) object); } else if (object instanceof Map) {