Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dd-openfeature/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ java {
dependencies {
api("dev.openfeature:sdk:1.23.0")
implementation("io.opentelemetry:opentelemetry-api:1.66.0")
// Linear-time regex engine for MATCHES and NOT_MATCHES, like the Rust evaluator.
implementation(libs.re2j)
api(libs.jackson.core)

testImplementation(libs.bundles.junit5)
Expand Down
1 change: 1 addition & 0 deletions dd-openfeature/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ com.github.stephenc.jcip:jcip-annotations:1.0-1=spotbugs
com.google.code.findbugs:jsr305:3.0.2=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,spotbugs,testCompileClasspath,testRuntimeClasspath
com.google.code.gson:gson:2.14.0=spotbugs
com.google.errorprone:error_prone_annotations:2.48.0=spotbugs
com.google.re2j:re2j:1.8=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.thoughtworks.qdox:qdox:1.12.1=codenarc
commons-io:commons-io:2.21.0=spotbugs
de.thetaphi:forbiddenapis:3.11=compileClasspath,jmhCompileClasspath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
* Evaluation cost on the opt-in scale suites of the shared FFE fixtures ({@code
* evaluation-suites/scale} and {@code evaluation-suites/scale-json}): large lists, regex, rules,
* splits and shards, time windows, many variations and flags, and JSON values up to 10 MB. The
* dimensions are listed in {@code evaluation-suites/scale/AGENTS.md} of the fixtures.
* dimensions are listed in {@code evaluation-suites/scale/AGENTS.md} of the fixtures. The {@code
* regex} suite adds literal patterns and inputs that make a backtracking engine slow; it needs the
* linear-time matcher.
*
* <p>Each operation evaluates a case of the selected case file through the provider. With {@code
* index=all}, the operations go through the cases of the file in turn, so a row is the mean of its
Expand Down Expand Up @@ -73,7 +75,9 @@ public class ScaleSuiteBenchmark {
"scale/wide-context",
"scale-json/json-shapes",
"scale-json/json-sizes",
"scale-json/json-values"
"scale-json/json-values",
"regex/backtracking",
"regex/literals"
})
public String cases;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.datadog.openfeature.internal.ufc;

import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import java.util.regex.Pattern;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

/**
* Compares {@code java.util.regex} {@code find()}, which the evaluator used before, with {@link
* RegexMatcher}, for each matcher tier. Each pattern shape comes from real UFC targeting rules.
*/
@State(Scope.Thread)
@Warmup(iterations = 3, time = 1, timeUnit = SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = SECONDS)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(NANOSECONDS)
@Fork(1)
public class RegexMatcherBenchmark {
/** The pattern shape. */
@Param({"literal-suffix", "wildcard-alternation", "char-class", "complex", "word-boundary"})
public String shape;

/** A matching input, a short input that does not match, or a 2,000-character miss. */
@Param({"hit", "miss", "long-miss"})
public String input;

private Pattern jdk;
private RegexMatcher linear;
private String value;

@Setup
public void setup() {
final String pattern;
final String hit;
final String miss;
switch (shape) {
case "literal-suffix":
pattern = "@example\\.com$";
hit = "jane.doe@example.com";
miss = "jane.doe@example.org";
break;
case "wildcard-alternation":
pattern = ".*@(site-a|site-b)\\.example$";
hit = "jane.doe@site-b.example";
miss = "jane.doe@site-c.example";
break;
case "char-class":
pattern = "^[a-zA-Z0-9._%+-]+@site\\.test$";
hit = "jane.doe+x@site.test";
miss = "jane.doe+x@other.test";
break;
case "complex":
pattern = "^u[0-9]{1,6}@(?:alpha|bravo|charlie)\\.(?:io|net)$";
hit = "u12345@bravo.net";
miss = "u12345@delta.net";
break;
case "word-boundary":
pattern = "\\bma\u00f1ana\\b";
hit = "llega ma\u00f1ana temprano";
miss = "llega pronto";
break;
default:
throw new IllegalArgumentException(shape);
}
jdk = Pattern.compile(pattern);
linear = RegexMatcher.compile(pattern);
switch (input) {
case "hit":
value = hit;
break;
case "miss":
value = miss;
break;
default:
final StringBuilder s = new StringBuilder(2_000 + miss.length());
for (int i = 0; i < 2_000; i++) {
s.append('a');
}
value = s.append(miss).toString();
break;
}
}

@Benchmark
public boolean javaUtilRegex() {
return jdk.matcher(value).find();
}

@Benchmark
public boolean regexMatcher() {
return linear.matches(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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.RegexMatcher;
import com.datadog.openfeature.internal.ufc.Rule;
import com.datadog.openfeature.internal.ufc.ServerConfiguration;
import com.datadog.openfeature.internal.ufc.Shard;
Expand All @@ -41,7 +42,6 @@
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -348,13 +348,13 @@ private static boolean evaluateCondition(

private static boolean matchesRegex(
final Object attributeValue, final ConditionConfiguration condition) {
// Parsed configurations cache the validated pattern. Directly constructed configurations
// Parsed configurations cache the validated matcher. Directly constructed configurations
// retain the same syntax and error handling.
final Pattern pattern =
final RegexMatcher regex =
condition.regex != null
? condition.regex
: ConditionConfiguration.compileRegex(condition.value);
return pattern.matcher(String.valueOf(attributeValue)).find();
return regex.matches(String.valueOf(attributeValue));
}

private static boolean isOneOf(final Object attributeValue, final Object conditionValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.datadog.openfeature.internal.ufc;

import java.util.regex.Pattern;

public final class ConditionConfiguration {
public final ConditionOperator operator;
public final String attribute;
Expand All @@ -11,8 +9,8 @@ public final class ConditionConfiguration {
// (not from JSON) when the operator is a SEMVER_* operator.
public transient ParsedSemver semverComparand;

// The validated MATCHES/NOT_MATCHES pattern, prepared when the configuration is parsed.
public transient Pattern regex;
// The validated MATCHES/NOT_MATCHES matcher, prepared when the configuration is parsed.
public transient RegexMatcher regex;

public ConditionConfiguration(
final ConditionOperator operator, final String attribute, final Object value) {
Expand All @@ -21,18 +19,12 @@ public ConditionConfiguration(
this.value = value;
}

/** Compiles the Java pattern after translating the supported POSIX character classes. */
public static Pattern compileRegex(final Object value) {
return Pattern.compile(normalizeRegex(String.valueOf(value)));
}

private static String normalizeRegex(final String regex) {
return regex
.replace("[:alnum:]", "\\p{Alnum}")
.replace("[:alpha:]", "\\p{Alpha}")
.replace("[:digit:]", "\\p{Digit}")
.replace("[:lower:]", "\\p{Lower}")
.replace("[:upper:]", "\\p{Upper}")
.replace("[:space:]", "\\p{Space}");
/**
* Compiles a MATCHES/NOT_MATCHES pattern.
*
* @throws java.util.regex.PatternSyntaxException if the pattern is malformed.
*/
public static RegexMatcher compileRegex(final Object value) {
return RegexMatcher.compile(String.valueOf(value));
}
}
Loading
Loading