From 54772e04c9f0872db896840e014509faef994a2d Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Tue, 17 Nov 2026 02:40:41 +0000 Subject: [PATCH 1/3] Resolve each attribute once per evaluation and prepare split features at load A flag with many allocations re-read the same context attributes in every allocation: a context lookup and a value conversion each time, and the conversion boxes numbers. Each evaluation now keeps the attributes it has resolved, so a flag with 1,000 allocations reads its attribute once. A matched split's features were turned into metadata entries on every evaluation, concatenating a prefixed key for each feature and destination. The split now resolves its entries once, when it is built, and an evaluation only copies them. Unknown destinations are now reported when the configuration loads, still once per name. Co-Authored-By: Claude Opus 5.5 --- .../internal/evaluation/EvaluationScope.java | 71 +++++++++ .../internal/evaluation/FlagEvaluator.java | 106 +++----------- .../internal/evaluation/SplitFeatures.java | 135 ++++++++++++++++++ .../openfeature/internal/ufc/Split.java | 5 + .../evaluation/EvaluationScopeTest.java | 77 ++++++++++ .../evaluation/SplitFeaturesTest.java | 118 +++++++++++++++ 6 files changed, 425 insertions(+), 87 deletions(-) create mode 100644 dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java create mode 100644 dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/SplitFeatures.java create mode 100644 dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/EvaluationScopeTest.java create mode 100644 dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/SplitFeaturesTest.java diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java new file mode 100644 index 00000000000..2f6f3d0d664 --- /dev/null +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java @@ -0,0 +1,71 @@ +package com.datadog.openfeature.internal.evaluation; + +import dev.openfeature.sdk.EvaluationContext; +import java.util.HashMap; +import java.util.Map; + +/** + * The state of one flag evaluation. It resolves each context attribute once, however many + * conditions read it: a flag with many allocations reads the same attributes in each of them, and + * resolving one means a context lookup and a value conversion that can allocate. + */ +final class EvaluationScope { + private static final String ID = "id"; + private static final Object NULL = new Object(); + // A few attributes are compared in place; more go to a map, so that the lookups stay constant. + private static final int MAX_SCANNED = 8; + + final EvaluationContext context; + private String[] names; + private Object[] values; + private int size; + private Map overflow; + + EvaluationScope(final EvaluationContext context) { + this.context = context; + } + + /** Returns the attribute's value, or {@code null} when the context does not hold it. */ + Object attribute(final String name) { + for (int i = 0; i < size; i++) { + final String cached = names[i]; + if (cached == name || cached.equals(name)) { + final Object value = values[i]; + return value == NULL ? null : value; + } + } + if (overflow != null) { + final Object value = overflow.get(name); + if (value != null) { + return value == NULL ? null : value; + } + } + final Object value = resolve(name); + remember(name, value == null ? NULL : value); + return value; + } + + private Object resolve(final String name) { + // Special handling for "id" attribute: if not explicitly provided, use targeting key + if (ID.equals(name) && !context.keySet().contains(name)) { + return context.getTargetingKey(); + } + return context.convertValue(context.getValue(name)); + } + + private void remember(final String name, final Object value) { + if (names == null) { + names = new String[MAX_SCANNED]; + values = new Object[MAX_SCANNED]; + } else if (size == MAX_SCANNED) { + if (overflow == null) { + overflow = new HashMap<>(); + } + overflow.put(name, value); + return; + } + names[size] = name; + values[size] = value; + size++; + } +} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java index 55ba7dc45ce..5eb62be1c90 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java @@ -2,12 +2,8 @@ import static com.datadog.openfeature.internal.evaluation.MetadataKeys.ALLOCATION_KEY; import static com.datadog.openfeature.internal.evaluation.MetadataKeys.DO_LOG; -import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURE_PREFIX; import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVAL_TIMESTAMP_MS; import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EXPOSURE_CACHE_HIT; -import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EXPOSURE_FEATURE_PREFIX; -import static com.datadog.openfeature.internal.evaluation.MetadataKeys.FEATURE_PREFIX; -import static com.datadog.openfeature.internal.evaluation.MetadataKeys.HAS_EVALUATION_FEATURES; import static com.datadog.openfeature.internal.evaluation.MetadataKeys.OBSERVE_FULL_EVALUATION_DATA; import static com.datadog.openfeature.internal.evaluation.MetadataKeys.SPLIT_SERIAL_ID; import static java.util.Arrays.asList; @@ -16,7 +12,6 @@ import com.datadog.openfeature.internal.ufc.Allocation; import com.datadog.openfeature.internal.ufc.ConditionConfiguration; import com.datadog.openfeature.internal.ufc.ConditionOperator; -import com.datadog.openfeature.internal.ufc.Feature; import com.datadog.openfeature.internal.ufc.Flag; import com.datadog.openfeature.internal.ufc.ParsedSemver; import com.datadog.openfeature.internal.ufc.Rule; @@ -43,12 +38,9 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** Local evaluation over a single configuration snapshot. No transport dependency. */ public final class FlagEvaluator { - private static final Logger LOGGER = LoggerFactory.getLogger(FlagEvaluator.class); private static final Set> SUPPORTED_RESOLUTION_TYPES = new HashSet<>(asList(String.class, Boolean.class, Integer.class, Double.class, Value.class)); static final String FEATURE_DESTINATION_HOOK = "HOOK"; @@ -158,8 +150,12 @@ public ProviderEvaluation evaluate( final Instant now = Instant.now(); final long evalTimestampMs = now.toEpochMilli(); final String targetingKey = context.getTargetingKey(); + EvaluationScope scope = null; - for (final Allocation allocation : flag.allocations) { + // Indexed loops: a flag can hold thousands of allocations, scanned on every evaluation. + final List allocations = flag.allocations; + for (int a = 0; a < allocations.size(); a++) { + final Allocation allocation = allocations.get(a); if (experimentationCutoff != null && ExperimentationOptOut.excludes( experimentationOptOutMode, experimentationCutoff, allocation)) { @@ -170,7 +166,10 @@ public ProviderEvaluation evaluate( } if (!isEmpty(allocation.rules)) { - if (!evaluateRules(allocation.rules, context)) { + if (scope == null) { + scope = new EvaluationScope(context); + } + if (!evaluateRules(allocation.rules, scope)) { continue; } } @@ -276,15 +275,16 @@ public static boolean isAllocationActive(final Allocation allocation, final Inst return true; } - private static boolean evaluateRules(final List rules, final EvaluationContext context) { - for (final Rule rule : rules) { - if (isEmpty(rule.conditions)) { + private static boolean evaluateRules(final List rules, final EvaluationScope scope) { + for (int r = 0; r < rules.size(); r++) { + final List conditions = rules.get(r).conditions; + if (isEmpty(conditions)) { continue; } boolean allConditionsMatch = true; - for (final ConditionConfiguration condition : rule.conditions) { - if (!evaluateCondition(condition, context)) { + for (int c = 0; c < conditions.size(); c++) { + if (!evaluateCondition(conditions.get(c), scope)) { allConditionsMatch = false; break; } @@ -298,16 +298,16 @@ private static boolean evaluateRules(final List rules, final EvaluationCon } private static boolean evaluateCondition( - final ConditionConfiguration condition, final EvaluationContext context) { + final ConditionConfiguration condition, final EvaluationScope scope) { if (condition.operator == ConditionOperator.IS_NULL) { - final Object value = resolveAttribute(condition.attribute, context); + final Object value = scope.attribute(condition.attribute); boolean isNull = value == null; // condition.value determines if we're checking for null (true) or not null (false) boolean expectedNull = condition.value instanceof Boolean ? (Boolean) condition.value : true; return isNull == expectedNull; } - final Object attributeValue = resolveAttribute(condition.attribute, context); + final Object attributeValue = scope.attribute(condition.attribute); if (attributeValue == null) { return false; } @@ -510,7 +510,7 @@ private ProviderEvaluation resolveVariant( } metadataBuilder.addBoolean(DO_LOG, doLog); } - addSplitFeatures(metadataBuilder, split); + split.preparedFeatures.addTo(metadataBuilder); if (doLog) { metadataBuilder.addBoolean( EXPOSURE_CACHE_HIT, @@ -538,74 +538,6 @@ && isEmpty(split.shards)) { return !isEmpty(split.shards) ? Reason.SPLIT.name() : Reason.STATIC.name(); } - private static void addSplitFeatures( - final ImmutableMetadata.ImmutableMetadataBuilder metadataBuilder, final Split split) { - if (split.features == null) { - return; - } - Set seenKeys = null; - boolean evaluationFeatures = false; - for (final Feature feature : split.features) { - if (feature == null || feature.key == null || feature.destinations == null) { - continue; - } - if (seenKeys == null) { - seenKeys = new HashSet<>(); - } - // The first feature with a key wins, for every destination. - if (!seenKeys.add(feature.key)) { - continue; - } - for (final String destination : feature.destinations) { - final String prefix = metadataPrefix(destination); - if (prefix != null) { - addFeature(metadataBuilder, prefix + feature.key, feature.value); - evaluationFeatures |= EVALUATION_FEATURE_PREFIX.equals(prefix); - } else if (WARNED_FEATURE_DESTINATIONS.size() < MAX_WARNED_FEATURE_DESTINATIONS - && WARNED_FEATURE_DESTINATIONS.add(destination)) { - LOGGER.warn("Ignoring unknown feature destination {}", destination); - } - } - } - if (evaluationFeatures) { - metadataBuilder.addBoolean(HAS_EVALUATION_FEATURES, true); - } - } - - private static String metadataPrefix(final String destination) { - if (FEATURE_DESTINATION_HOOK.equals(destination)) { - return FEATURE_PREFIX; - } - if (FEATURE_DESTINATION_EXPOSURE.equals(destination)) { - return EXPOSURE_FEATURE_PREFIX; - } - if (FEATURE_DESTINATION_EVALUATION.equals(destination)) { - return EVALUATION_FEATURE_PREFIX; - } - return null; - } - - private static void addFeature( - final ImmutableMetadata.ImmutableMetadataBuilder metadataBuilder, - final String key, - final Object value) { - if (value instanceof String) { - metadataBuilder.addString(key, (String) value); - } else if (value instanceof Boolean) { - metadataBuilder.addBoolean(key, (Boolean) value); - } else if (value instanceof Number) { - metadataBuilder.addDouble(key, ((Number) value).doubleValue()); - } - } - - private static Object resolveAttribute(final String name, final EvaluationContext context) { - // Special handling for "id" attribute: if not explicitly provided, use targeting key - if ("id".equals(name) && !context.keySet().contains(name)) { - return context.getTargetingKey(); - } - return context.convertValue(context.getValue(name)); - } - public static boolean isTypeCompatible(final Class target, final ValueType variationType) { if (variationType == null) { return true; // No type info — allow any diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/SplitFeatures.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/SplitFeatures.java new file mode 100644 index 00000000000..c3f00ecabe9 --- /dev/null +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/SplitFeatures.java @@ -0,0 +1,135 @@ +package com.datadog.openfeature.internal.evaluation; + +import static com.datadog.openfeature.internal.evaluation.FlagEvaluator.FEATURE_DESTINATION_EVALUATION; +import static com.datadog.openfeature.internal.evaluation.FlagEvaluator.FEATURE_DESTINATION_EXPOSURE; +import static com.datadog.openfeature.internal.evaluation.FlagEvaluator.FEATURE_DESTINATION_HOOK; +import static com.datadog.openfeature.internal.evaluation.FlagEvaluator.MAX_WARNED_FEATURE_DESTINATIONS; +import static com.datadog.openfeature.internal.evaluation.FlagEvaluator.WARNED_FEATURE_DESTINATIONS; +import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURE_PREFIX; +import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EXPOSURE_FEATURE_PREFIX; +import static com.datadog.openfeature.internal.evaluation.MetadataKeys.FEATURE_PREFIX; +import static com.datadog.openfeature.internal.evaluation.MetadataKeys.HAS_EVALUATION_FEATURES; + +import com.datadog.openfeature.internal.ufc.Feature; +import dev.openfeature.sdk.ImmutableMetadata; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The metadata entries of a split's features, resolved once when the split is built: the first + * feature of each key wins, each destination gets its prefixed key, and unknown destinations are + * dropped. An evaluation then only copies the entries. + */ +public final class SplitFeatures { + private static final Logger LOGGER = LoggerFactory.getLogger(SplitFeatures.class); + + /** Prepared features of a split without any. */ + public static final SplitFeatures NONE = new SplitFeatures(new String[0], new Object[0], false); + + private final String[] keys; + + /** A {@link String}, {@link Boolean} or {@link Double}. */ + private final Object[] values; + + private final boolean hasEvaluationFeatures; + + private SplitFeatures( + final String[] keys, final Object[] values, final boolean hasEvaluationFeatures) { + this.keys = keys; + this.values = values; + this.hasEvaluationFeatures = hasEvaluationFeatures; + } + + /** + * Resolves the metadata entries of features. + * + * @param features the split's features, or {@code null}. + * @return the prepared entries, {@link #NONE} if there are none. + */ + public static SplitFeatures prepare(final List features) { + if (features == null) { + return NONE; + } + final List keys = new ArrayList<>(); + final List values = new ArrayList<>(); + final Set seenKeys = new HashSet<>(); + boolean evaluationFeatures = false; + for (final Feature feature : features) { + if (feature == null || feature.key == null || feature.destinations == null) { + continue; + } + // The first feature with a key wins, for every destination. + if (!seenKeys.add(feature.key)) { + continue; + } + final Object value = metadataValue(feature.value); + for (final String destination : feature.destinations) { + final String prefix = metadataPrefix(destination); + if (prefix != null) { + if (value != null) { + keys.add(prefix + feature.key); + values.add(value); + } + evaluationFeatures |= EVALUATION_FEATURE_PREFIX.equals(prefix); + } else if (WARNED_FEATURE_DESTINATIONS.size() < MAX_WARNED_FEATURE_DESTINATIONS + && WARNED_FEATURE_DESTINATIONS.add(destination)) { + LOGGER.warn("Ignoring unknown feature destination {}", destination); + } + } + } + if (keys.isEmpty() && !evaluationFeatures) { + return NONE; + } + return new SplitFeatures( + keys.toArray(new String[0]), values.toArray(new Object[0]), evaluationFeatures); + } + + /** Adds the entries, and the evaluation-features marker when there are any, to metadata. */ + void addTo(final ImmutableMetadata.ImmutableMetadataBuilder metadataBuilder) { + for (int i = 0; i < keys.length; i++) { + final Object value = values[i]; + if (value instanceof String) { + metadataBuilder.addString(keys[i], (String) value); + } else if (value instanceof Boolean) { + metadataBuilder.addBoolean(keys[i], (Boolean) value); + } else { + metadataBuilder.addDouble(keys[i], (Double) value); + } + } + if (hasEvaluationFeatures) { + metadataBuilder.addBoolean(HAS_EVALUATION_FEATURES, true); + } + } + + /** The number of metadata entries, without the evaluation-features marker. */ + int size() { + return keys.length; + } + + private static Object metadataValue(final Object value) { + if (value instanceof String || value instanceof Boolean) { + return value; + } + if (value instanceof Number) { + return ((Number) value).doubleValue(); + } + return null; + } + + private static String metadataPrefix(final String destination) { + if (FEATURE_DESTINATION_HOOK.equals(destination)) { + return FEATURE_PREFIX; + } + if (FEATURE_DESTINATION_EXPOSURE.equals(destination)) { + return EXPOSURE_FEATURE_PREFIX; + } + if (FEATURE_DESTINATION_EVALUATION.equals(destination)) { + return EVALUATION_FEATURE_PREFIX; + } + return null; + } +} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Split.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Split.java index 0897b77a058..99fd8be7546 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Split.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Split.java @@ -1,5 +1,6 @@ package com.datadog.openfeature.internal.ufc; +import com.datadog.openfeature.internal.evaluation.SplitFeatures; import java.util.List; import java.util.Map; @@ -14,6 +15,9 @@ public final class Split { /** Null when the UFC split has no features. */ public final List features; + /** The metadata entries of {@link #features}, resolved once for every evaluation. */ + public final SplitFeatures preparedFeatures; + public Split( final List shards, final String variationKey, @@ -33,5 +37,6 @@ public Split( this.extraLogging = extraLogging; this.serialId = serialId; this.features = features; + this.preparedFeatures = SplitFeatures.prepare(features); } } diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/EvaluationScopeTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/EvaluationScopeTest.java new file mode 100644 index 00000000000..268a5e046d5 --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/EvaluationScopeTest.java @@ -0,0 +1,77 @@ +package com.datadog.openfeature.internal.evaluation; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import dev.openfeature.sdk.MutableContext; +import dev.openfeature.sdk.Value; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.Test; + +class EvaluationScopeTest { + + @Test + void resolvesEachAttributeOnce() { + final CountingContext context = new CountingContext("user-1"); + context.add("plan", "pro"); + final EvaluationScope scope = new EvaluationScope(context); + + assertEquals("pro", scope.attribute("plan")); + assertEquals("pro", scope.attribute(new String("plan"))); + assertNull(scope.attribute("missing")); + assertNull(scope.attribute("missing")); + + assertEquals(1, context.reads("plan")); + assertEquals(1, context.reads("missing")); + } + + @Test + void resolvesManyAttributesOnce() { + final CountingContext context = new CountingContext("user-1"); + for (int i = 0; i < 20; i++) { + context.add("attr" + i, i); + } + final EvaluationScope scope = new EvaluationScope(context); + + for (int round = 0; round < 2; round++) { + for (int i = 0; i < 20; i++) { + assertEquals(i, scope.attribute("attr" + i)); + } + assertNull(scope.attribute("missing")); + } + + for (int i = 0; i < 20; i++) { + assertEquals(1, context.reads("attr" + i)); + } + assertEquals(1, context.reads("missing")); + } + + @Test + void fallsBackToTheTargetingKeyForAnAbsentId() { + assertEquals("user-1", new EvaluationScope(new MutableContext("user-1")).attribute("id")); + + final MutableContext context = new MutableContext("user-1"); + context.add("id", "explicit"); + assertEquals("explicit", new EvaluationScope(context).attribute("id")); + } + + /** Counts the attribute reads of the evaluation. */ + private static final class CountingContext extends MutableContext { + private final Map reads = new HashMap<>(); + + CountingContext(final String targetingKey) { + super(targetingKey); + } + + @Override + public Value getValue(final String key) { + reads.merge(key, 1, Integer::sum); + return super.getValue(key); + } + + int reads(final String key) { + return reads.getOrDefault(key, 0); + } + } +} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/SplitFeaturesTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/SplitFeaturesTest.java new file mode 100644 index 00000000000..9f9e038157f --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/SplitFeaturesTest.java @@ -0,0 +1,118 @@ +package com.datadog.openfeature.internal.evaluation; + +import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EVALUATION_FEATURE_PREFIX; +import static com.datadog.openfeature.internal.evaluation.MetadataKeys.EXPOSURE_FEATURE_PREFIX; +import static com.datadog.openfeature.internal.evaluation.MetadataKeys.FEATURE_PREFIX; +import static com.datadog.openfeature.internal.evaluation.MetadataKeys.HAS_EVALUATION_FEATURES; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.datadog.openfeature.internal.ufc.Feature; +import com.datadog.openfeature.internal.ufc.Split; +import dev.openfeature.sdk.ImmutableMetadata; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +class SplitFeaturesTest { + + @AfterEach + void tearDown() { + FlagEvaluator.WARNED_FEATURE_DESTINATIONS.clear(); + } + + @Test + void preparesTheEntriesWhenTheSplitIsBuilt() { + final Split split = + new Split( + emptyList(), + "on", + emptyMap(), + 7, + asList( + new Feature("holdout.key", "q4-global", asList("HOOK", "EXPOSURE")), + new Feature("holdout.weight", 0.5, singletonList("EVALUATION")), + new Feature("holdout.enabled", true, singletonList("EXPOSURE")))); + + final Map expected = new HashMap<>(); + expected.put(FEATURE_PREFIX + "holdout.key", "q4-global"); + expected.put(EXPOSURE_FEATURE_PREFIX + "holdout.key", "q4-global"); + expected.put(EVALUATION_FEATURE_PREFIX + "holdout.weight", 0.5); + expected.put(EXPOSURE_FEATURE_PREFIX + "holdout.enabled", true); + expected.put(HAS_EVALUATION_FEATURES, true); + assertEquals(4, split.preparedFeatures.size()); + assertEquals(expected, metadata(split.preparedFeatures)); + } + + @Test + void keepsTheFirstFeatureOfAKeyAndSkipsIncompleteOrUnsupportedOnes() { + final SplitFeatures features = + SplitFeatures.prepare( + asList( + null, + new Feature(null, "x", singletonList("HOOK")), + new Feature("no.destination", "x", null), + new Feature("holdout.key", "q4-global", singletonList("HOOK")), + new Feature("holdout.key", "q1-global", asList("HOOK", "EXPOSURE")), + new Feature("unsupported", singletonList("x"), singletonList("HOOK")))); + + assertEquals(singletonMap(FEATURE_PREFIX + "holdout.key", "q4-global"), metadata(features)); + } + + @Test + void storesNumbersAsDoubles() { + final SplitFeatures features = + SplitFeatures.prepare(singletonList(new Feature("weight", 1, singletonList("HOOK")))); + + assertEquals(singletonMap(FEATURE_PREFIX + "weight", 1.0), metadata(features)); + } + + @Test + void marksEvaluationFeaturesEvenWhenTheirValueIsUnsupported() { + final SplitFeatures features = + SplitFeatures.prepare( + singletonList(new Feature("bad", singletonList("x"), singletonList("EVALUATION")))); + + assertEquals(singletonMap(HAS_EVALUATION_FEATURES, true), metadata(features)); + } + + @Test + void sharesTheEmptyPreparationForSplitsWithoutFeatures() { + assertSame(SplitFeatures.NONE, SplitFeatures.prepare(null)); + assertSame(SplitFeatures.NONE, SplitFeatures.prepare(emptyList())); + assertSame(SplitFeatures.NONE, new Split(emptyList(), "on", emptyMap(), null).preparedFeatures); + } + + @Test + void warnsAboutUnknownDestinationsWhenPreparing() { + final SplitFeatures features = + SplitFeatures.prepare( + asList( + new Feature("holdout.key", "q4-global", asList("HOOK", "NEW_A")), + new Feature("other", "x", asList("NEW_A", "NEW_B")))); + + assertEquals( + new HashSet<>(asList("NEW_A", "NEW_B")), FlagEvaluator.WARNED_FEATURE_DESTINATIONS); + assertEquals(singletonMap(FEATURE_PREFIX + "holdout.key", "q4-global"), metadata(features)); + assertTrue(metadata(SplitFeatures.prepare(null)).isEmpty()); + } + + private static Map singletonMap(final String key, final Object value) { + final Map map = new HashMap<>(); + map.put(key, value); + return map; + } + + private static Map metadata(final SplitFeatures features) { + final ImmutableMetadata.ImmutableMetadataBuilder builder = ImmutableMetadata.builder(); + features.addTo(builder); + return builder.build().asUnmodifiableMap(); + } +} From 25d7a13e097bbb5b6a9a5ce4f14d0649aebad58e Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Tue, 17 Nov 2026 02:43:23 +0000 Subject: [PATCH 2/3] Prepare split features when the split is first served The split caches what the evaluator derives from its features instead of building it in its constructor, so the configuration model does not depend on the evaluator and an unknown destination is still reported when a split is served, as before. The allocation and rule scans keep their for-each loops. Co-Authored-By: Claude Opus 5.5 --- .../internal/evaluation/EvaluationScope.java | 3 +++ .../internal/evaluation/FlagEvaluator.java | 16 +++++------- .../internal/evaluation/SplitFeatures.java | 12 ++++----- .../openfeature/internal/ufc/Split.java | 25 ++++++++++++++++--- .../evaluation/EvaluationScopeTest.java | 10 ++++++++ .../evaluation/SplitFeaturesTest.java | 12 ++++++--- 6 files changed, 54 insertions(+), 24 deletions(-) diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java index 2f6f3d0d664..cdf8c25eabb 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java @@ -8,6 +8,9 @@ * The state of one flag evaluation. It resolves each context attribute once, however many * conditions read it: a flag with many allocations reads the same attributes in each of them, and * resolving one means a context lookup and a value conversion that can allocate. + * + *

An attribute is read from the context the first time a condition needs it. A context that + * changes during the evaluation is not read again. */ final class EvaluationScope { private static final String ID = "id"; diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java index 5eb62be1c90..e2fc3627b25 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java @@ -152,10 +152,7 @@ public ProviderEvaluation evaluate( final String targetingKey = context.getTargetingKey(); EvaluationScope scope = null; - // Indexed loops: a flag can hold thousands of allocations, scanned on every evaluation. - final List allocations = flag.allocations; - for (int a = 0; a < allocations.size(); a++) { - final Allocation allocation = allocations.get(a); + for (final Allocation allocation : flag.allocations) { if (experimentationCutoff != null && ExperimentationOptOut.excludes( experimentationOptOutMode, experimentationCutoff, allocation)) { @@ -276,15 +273,14 @@ public static boolean isAllocationActive(final Allocation allocation, final Inst } private static boolean evaluateRules(final List rules, final EvaluationScope scope) { - for (int r = 0; r < rules.size(); r++) { - final List conditions = rules.get(r).conditions; - if (isEmpty(conditions)) { + for (final Rule rule : rules) { + if (isEmpty(rule.conditions)) { continue; } boolean allConditionsMatch = true; - for (int c = 0; c < conditions.size(); c++) { - if (!evaluateCondition(conditions.get(c), scope)) { + for (final ConditionConfiguration condition : rule.conditions) { + if (!evaluateCondition(condition, scope)) { allConditionsMatch = false; break; } @@ -510,7 +506,7 @@ private ProviderEvaluation resolveVariant( } metadataBuilder.addBoolean(DO_LOG, doLog); } - split.preparedFeatures.addTo(metadataBuilder); + split.preparedFeatures(SplitFeatures::prepare).addTo(metadataBuilder); if (doLog) { metadataBuilder.addBoolean( EXPOSURE_CACHE_HIT, diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/SplitFeatures.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/SplitFeatures.java index c3f00ecabe9..9a3a6d9cffe 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/SplitFeatures.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/SplitFeatures.java @@ -20,15 +20,15 @@ import org.slf4j.LoggerFactory; /** - * The metadata entries of a split's features, resolved once when the split is built: the first - * feature of each key wins, each destination gets its prefixed key, and unknown destinations are - * dropped. An evaluation then only copies the entries. + * The metadata entries of a split's features, resolved when the split is first served and then + * cached on it: the first feature of each key wins, each destination gets its prefixed key, and + * unknown destinations are dropped. Later evaluations only copy the entries. */ -public final class SplitFeatures { +final class SplitFeatures { private static final Logger LOGGER = LoggerFactory.getLogger(SplitFeatures.class); /** Prepared features of a split without any. */ - public static final SplitFeatures NONE = new SplitFeatures(new String[0], new Object[0], false); + static final SplitFeatures NONE = new SplitFeatures(new String[0], new Object[0], false); private final String[] keys; @@ -50,7 +50,7 @@ private SplitFeatures( * @param features the split's features, or {@code null}. * @return the prepared entries, {@link #NONE} if there are none. */ - public static SplitFeatures prepare(final List features) { + static SplitFeatures prepare(final List features) { if (features == null) { return NONE; } diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Split.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Split.java index 99fd8be7546..411cf0bff78 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Split.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/Split.java @@ -1,8 +1,8 @@ package com.datadog.openfeature.internal.ufc; -import com.datadog.openfeature.internal.evaluation.SplitFeatures; import java.util.List; import java.util.Map; +import java.util.function.Function; public final class Split { public final List shards; @@ -15,8 +15,8 @@ public final class Split { /** Null when the UFC split has no features. */ public final List features; - /** The metadata entries of {@link #features}, resolved once for every evaluation. */ - public final SplitFeatures preparedFeatures; + // What the evaluator derives from the features, built when the split is first served. + private volatile Object preparedFeatures; public Split( final List shards, @@ -37,6 +37,23 @@ public Split( this.extraLogging = extraLogging; this.serialId = serialId; this.features = features; - this.preparedFeatures = SplitFeatures.prepare(features); + } + + /** + * Returns what {@code prepare} derives from the features, computed once for the split. Two + * evaluations that serve the split first at the same time can both compute it; either result is + * kept. + * + * @param prepare derives the prepared form from {@link #features}, which can be {@code null}. + * @return the prepared form. + */ + @SuppressWarnings("unchecked") + public T preparedFeatures(final Function, T> prepare) { + Object prepared = preparedFeatures; + if (prepared == null) { + prepared = prepare.apply(features); + preparedFeatures = prepared; + } + return (T) prepared; } } diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/EvaluationScopeTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/EvaluationScopeTest.java index 268a5e046d5..90c82ed6345 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/EvaluationScopeTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/EvaluationScopeTest.java @@ -56,6 +56,16 @@ void fallsBackToTheTargetingKeyForAnAbsentId() { assertEquals("explicit", new EvaluationScope(context).attribute("id")); } + @Test + void keepsAnExplicitNullIdWithoutFallingBackToTheTargetingKey() { + final Map attributes = new HashMap<>(); + attributes.put("id", new Value()); + final EvaluationScope scope = new EvaluationScope(new MutableContext("user-1", attributes)); + + assertNull(scope.attribute("id")); + assertNull(scope.attribute("id")); + } + /** Counts the attribute reads of the evaluation. */ private static final class CountingContext extends MutableContext { private final Map reads = new HashMap<>(); diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/SplitFeaturesTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/SplitFeaturesTest.java index 9f9e038157f..43b45ef71df 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/SplitFeaturesTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/evaluation/SplitFeaturesTest.java @@ -29,7 +29,7 @@ void tearDown() { } @Test - void preparesTheEntriesWhenTheSplitIsBuilt() { + void preparesTheEntriesOnceForTheSplit() { final Split split = new Split( emptyList(), @@ -47,8 +47,10 @@ void preparesTheEntriesWhenTheSplitIsBuilt() { expected.put(EVALUATION_FEATURE_PREFIX + "holdout.weight", 0.5); expected.put(EXPOSURE_FEATURE_PREFIX + "holdout.enabled", true); expected.put(HAS_EVALUATION_FEATURES, true); - assertEquals(4, split.preparedFeatures.size()); - assertEquals(expected, metadata(split.preparedFeatures)); + final SplitFeatures prepared = split.preparedFeatures(SplitFeatures::prepare); + assertSame(prepared, split.preparedFeatures(SplitFeatures::prepare)); + assertEquals(4, prepared.size()); + assertEquals(expected, metadata(prepared)); } @Test @@ -87,7 +89,9 @@ void marksEvaluationFeaturesEvenWhenTheirValueIsUnsupported() { void sharesTheEmptyPreparationForSplitsWithoutFeatures() { assertSame(SplitFeatures.NONE, SplitFeatures.prepare(null)); assertSame(SplitFeatures.NONE, SplitFeatures.prepare(emptyList())); - assertSame(SplitFeatures.NONE, new Split(emptyList(), "on", emptyMap(), null).preparedFeatures); + assertSame( + SplitFeatures.NONE, + new Split(emptyList(), "on", emptyMap(), null).preparedFeatures(SplitFeatures::prepare)); } @Test From 462d85cc41a6085817441736def66803def1ad9e Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Tue, 17 Nov 2026 02:53:48 +0000 Subject: [PATCH 3/3] Keep the first resolved attribute without allocating arrays Most flags read one attribute, so the evaluation scope keeps it in fields and allocates its arrays only for a second attribute. Co-Authored-By: Claude Opus 5.5 --- .../internal/evaluation/EvaluationScope.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java index cdf8c25eabb..e8fd29752e0 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java @@ -19,6 +19,9 @@ final class EvaluationScope { private static final int MAX_SCANNED = 8; final EvaluationContext context; + // Most flags read one attribute: it is kept without allocating the arrays. + private String firstName; + private Object firstValue; private String[] names; private Object[] values; private int size; @@ -30,6 +33,10 @@ final class EvaluationScope { /** Returns the attribute's value, or {@code null} when the context does not hold it. */ Object attribute(final String name) { + final String first = firstName; + if (first == name || (first != null && first.equals(name))) { + return firstValue == NULL ? null : firstValue; + } for (int i = 0; i < size; i++) { final String cached = names[i]; if (cached == name || cached.equals(name)) { @@ -57,6 +64,11 @@ private Object resolve(final String name) { } private void remember(final String name, final Object value) { + if (firstName == null) { + firstName = name; + firstValue = value; + return; + } if (names == null) { names = new String[MAX_SCANNED]; values = new Object[MAX_SCANNED];