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..e8fd29752e0
--- /dev/null
+++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/EvaluationScope.java
@@ -0,0 +1,86 @@
+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.
+ *
+ *
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";
+ 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;
+ // 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;
+ 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) {
+ 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)) {
+ 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 (firstName == null) {
+ firstName = name;
+ firstValue = value;
+ return;
+ }
+ 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..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
@@ -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,6 +150,7 @@ 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) {
if (experimentationCutoff != null
@@ -170,7 +163,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,7 +272,7 @@ public static boolean isAllocationActive(final Allocation allocation, final Inst
return true;
}
- private static boolean evaluateRules(final List rules, final EvaluationContext context) {
+ private static boolean evaluateRules(final List rules, final EvaluationScope scope) {
for (final Rule rule : rules) {
if (isEmpty(rule.conditions)) {
continue;
@@ -284,7 +280,7 @@ private static boolean evaluateRules(final List rules, final EvaluationCon
boolean allConditionsMatch = true;
for (final ConditionConfiguration condition : rule.conditions) {
- if (!evaluateCondition(condition, context)) {
+ if (!evaluateCondition(condition, scope)) {
allConditionsMatch = false;
break;
}
@@ -298,16 +294,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 +506,7 @@ private ProviderEvaluation resolveVariant(
}
metadataBuilder.addBoolean(DO_LOG, doLog);
}
- addSplitFeatures(metadataBuilder, split);
+ split.preparedFeatures(SplitFeatures::prepare).addTo(metadataBuilder);
if (doLog) {
metadataBuilder.addBoolean(
EXPOSURE_CACHE_HIT,
@@ -538,74 +534,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..9a3a6d9cffe
--- /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 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.
+ */
+final class SplitFeatures {
+ private static final Logger LOGGER = LoggerFactory.getLogger(SplitFeatures.class);
+
+ /** Prepared features of a split without any. */
+ 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.
+ */
+ static SplitFeatures prepare(final List features) {
+ if (features == null) {
+ return NONE;
+ }
+ final List keys = new ArrayList<>();
+ final List