From 8f8aeacab9c8bb62dbfa7495e175666199f3bdee Mon Sep 17 00:00:00 2001 From: Tom Fenwick <3538026+TSFenwick@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:44:06 -0800 Subject: [PATCH 1/2] perf: avoid copying the evaluation context toLdContext called asMap() twice on the evaluation context, and once more on each nested structure of a multi-kind context. Each call returns a new copy of the map, on every flag evaluation. The converter only reads these maps. It now reads them through asUnmodifiableMap(), which OpenFeature SDK 1.16.0 already has, and it reuses the top-level map. --- .../serverprovider/EvaluationContextConverter.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java b/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java index ef356a1..2414f0f 100644 --- a/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java +++ b/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java @@ -36,7 +36,7 @@ public EvaluationContextConverter(LDLogger logger) { public LDContext toLdContext(EvaluationContext evaluationContext) { // Using the kind as a map here because getting a value from an immutable context that doesn't exist // throws. https://github.com/open-feature/java-sdk/pull/300 - Map attributes = evaluationContext.asMap(); + Map attributes = evaluationContext.asUnmodifiableMap(); Value kindAsValue = attributes.get("kind"); @@ -45,7 +45,7 @@ public LDContext toLdContext(EvaluationContext evaluationContext) { String kindString = kindAsValue.asString(); if (Objects.equals(kindString, "multi")) { // A multi-context. - return BuildMultiContext(evaluationContext); + return BuildMultiContext(attributes); } else { // Single context with specified kind. finalKind = kindString; @@ -60,7 +60,7 @@ public LDContext toLdContext(EvaluationContext evaluationContext) { targetingKey = getTargetingKey(targetingKey, keyAsValue); - return BuildSingleContext(evaluationContext.asMap(), finalKind, targetingKey); + return BuildSingleContext(attributes, finalKind, targetingKey); } private boolean isNullOrEmpty(String value) { @@ -103,13 +103,13 @@ private String getTargetingKey(String targetingKey, Value keyAsValue) { /** * Build a multi-context from an evaluation context. * - * @param evaluationContext The evaluation context containing multi-context information. + * @param contextAttributes The attributes of the evaluation context containing multi-context information. * @return The built context. */ - private LDContext BuildMultiContext(EvaluationContext evaluationContext) { + private LDContext BuildMultiContext(Map contextAttributes) { ContextMultiBuilder multiBuilder = LDContext.multiBuilder(); - evaluationContext.asMap().forEach((kind, attributes) -> { + contextAttributes.forEach((kind, attributes) -> { // Do not need to do anything for the kind key. if (Objects.equals(kind, "kind")) return; @@ -119,7 +119,7 @@ private LDContext BuildMultiContext(EvaluationContext evaluationContext) { return; } - Map attributesMap = attributes.asStructure().asMap(); + Map attributesMap = attributes.asStructure().asUnmodifiableMap(); Value keyAsValue = attributesMap.get("key"); Value targetingKeyAsValue = attributesMap.get("targetingKey"); String targetingKey = targetingKeyAsValue != null ? targetingKeyAsValue.asString() : ""; From d4a806e4750f415d4f6f12118afc8adb1d20f9ee Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 30 Nov 2026 23:25:21 +0000 Subject: [PATCH 2/2] perf: read nested structures without copying Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../serverprovider/EvaluationContextConverter.java | 2 -- .../openfeature/serverprovider/ValueConverter.java | 2 +- .../serverprovider/ValueConverterTest.java | 14 ++++++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java b/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java index 2414f0f..d4700ff 100644 --- a/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java +++ b/src/main/java/com/launchdarkly/openfeature/serverprovider/EvaluationContextConverter.java @@ -34,8 +34,6 @@ public EvaluationContextConverter(LDLogger logger) { * @return An LDContext containing information from the evaluation context. */ public LDContext toLdContext(EvaluationContext evaluationContext) { - // Using the kind as a map here because getting a value from an immutable context that doesn't exist - // throws. https://github.com/open-feature/java-sdk/pull/300 Map attributes = evaluationContext.asUnmodifiableMap(); Value kindAsValue = attributes.get("kind"); diff --git a/src/main/java/com/launchdarkly/openfeature/serverprovider/ValueConverter.java b/src/main/java/com/launchdarkly/openfeature/serverprovider/ValueConverter.java index d0c6bfd..871a0ef 100644 --- a/src/main/java/com/launchdarkly/openfeature/serverprovider/ValueConverter.java +++ b/src/main/java/com/launchdarkly/openfeature/serverprovider/ValueConverter.java @@ -51,7 +51,7 @@ public LDValue toLdValue(Value value) { if (value.isStructure()) { ObjectBuilder objectBuilder = LDValue.buildObject(); Structure structure = value.asStructure(); - structure.asMap().forEach((itemKey, itemValue) -> { + structure.asUnmodifiableMap().forEach((itemKey, itemValue) -> { LDValue itemLdValue = toLdValue(itemValue); objectBuilder.put(itemKey, itemLdValue); }); diff --git a/src/test/java/com/launchdarkly/openfeature/serverprovider/ValueConverterTest.java b/src/test/java/com/launchdarkly/openfeature/serverprovider/ValueConverterTest.java index d1c84cc..5a8bf6c 100644 --- a/src/test/java/com/launchdarkly/openfeature/serverprovider/ValueConverterTest.java +++ b/src/test/java/com/launchdarkly/openfeature/serverprovider/ValueConverterTest.java @@ -4,15 +4,19 @@ import com.launchdarkly.sdk.LDValue; import com.launchdarkly.sdk.LDValueType; import dev.openfeature.sdk.ImmutableStructure; +import dev.openfeature.sdk.Structure; import dev.openfeature.sdk.Value; import org.junit.jupiter.api.Test; import java.time.Instant; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ValueConverterTest { private final ValueConverter valueConverter = new ValueConverter(LDLogger.none()); @@ -107,4 +111,14 @@ public void itCanConvertStructures() { assertEquals("bKey", valueList.get(1).keys().iterator().next()); assertEquals("bValue", valueList.get(1).values().iterator().next().stringValue()); } + + @Test + public void itUsesTheReadOnlyStructureMap() { + Structure structure = mock(Structure.class); + when(structure.asUnmodifiableMap()).thenReturn(Collections.singletonMap("key", new Value("value"))); + + LDValue converted = valueConverter.toLdValue(new Value(structure)); + + assertEquals("value", converted.get("key").stringValue()); + } }