diff --git a/src/jmh/java/benchmark/BenchmarkUtils.java b/src/jmh/java/benchmark/BenchmarkUtils.java index c8dda196d0..99f9562c48 100644 --- a/src/jmh/java/benchmark/BenchmarkUtils.java +++ b/src/jmh/java/benchmark/BenchmarkUtils.java @@ -1,5 +1,7 @@ package benchmark; +import org.openjdk.jmh.infra.Blackhole; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -81,4 +83,7 @@ private static int getRunForMillis() { } } + public static Blackhole blackHole() { + return new Blackhole("Today's password is swordfish. I understand instantiating Blackholes directly is dangerous."); + } } diff --git a/src/jmh/java/graphql/schema/FlyWeightBenchmark.java b/src/jmh/java/graphql/schema/FlyWeightBenchmark.java new file mode 100644 index 0000000000..69ce9b1df7 --- /dev/null +++ b/src/jmh/java/graphql/schema/FlyWeightBenchmark.java @@ -0,0 +1,119 @@ +package graphql.schema; + +import benchmark.BenchmarkUtils; +import graphql.util.flyweight.FlyweightKit; +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.Warmup; +import org.openjdk.jmh.infra.Blackhole; +import org.openjdk.jmh.profile.GCProfiler; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.concurrent.TimeUnit; + +@Warmup(iterations = 2, time = 5, batchSize = 50) +@Measurement(iterations = 3, batchSize = 50) +@Fork(3) +public class FlyWeightBenchmark { + + private static final int LOOPS = 10; + + static FlyweightKit.BiKeyMap flyweightFieldCoordinates = new FlyweightKit.BiKeyMap<>(); + static FlyweightKit.TriKeyMap FLYWEIGHT_CACHE = new FlyweightKit.TriKeyMap<>(); + + @Benchmark + @BenchmarkMode(Mode.Throughput) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + public void benchMarkDirectInstantiationThroughput(Blackhole blackhole) { + directInstantiation(blackhole); + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + public void benchMarkDirectInstantiationAvgTime(Blackhole blackhole) { + directInstantiation(blackhole); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + public void benchMarkUsingFlyweightsThroughput(Blackhole blackhole) { + flyweights(blackhole); + } + + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + public void benchMarkUsingFlyweightsAveTime(Blackhole blackhole) { + flyweights(blackhole); + } + + private void directInstantiation(Blackhole blackhole) { + for (int i = 0; i < LOOPS; i++) { + FieldCoordinates fieldCoordinates = FieldCoordinates.coordinates( + mkName("t", i), + "f"); + blackhole.consume(fieldCoordinates); + + PropertyFetchingImpl.CacheKey cacheKey = new PropertyFetchingImpl.CacheKey( + getClass().getClassLoader(), + mkName("c", i), + "f"); + blackhole.consume(cacheKey); + } + } + + private void flyweights(Blackhole blackhole) { + for (int i = 0; i < LOOPS; i++) { + FieldCoordinates fieldCoordinates = flyweightFieldCoordinates.computeIfAbsent( + mkName("t", i), + "f", + FieldCoordinates::coordinates); + blackhole.consume(fieldCoordinates); + + PropertyFetchingImpl.CacheKey cacheKey = FLYWEIGHT_CACHE.computeIfAbsent( + getClass().getClassLoader(), + mkName("c", i), + "f", + PropertyFetchingImpl.CacheKey::new); + blackhole.consume(cacheKey); + } + } + + private String mkName(String s, int i) { + return s + "_" + (i % 2 == 0); + } + + + public static void main(String[] args) throws RunnerException { + runAtStartup(); + Options opt = new OptionsBuilder() + .include("graphql.schema.FlyWeightBenchmark") + .addProfiler(GCProfiler.class) + .build(); + + new Runner(opt).run(); + } + + private static void runAtStartup() { + FlyWeightBenchmark benchMark = new FlyWeightBenchmark(); + Blackhole blackhole = BenchmarkUtils.blackHole(); + + BenchmarkUtils.runInToolingForSomeTimeThenExit( + () -> { + }, + () -> benchMark.benchMarkUsingFlyweightsThroughput(blackhole), + () -> { + } + + ); + } +} diff --git a/src/main/java/graphql/schema/GraphQLCodeRegistry.java b/src/main/java/graphql/schema/GraphQLCodeRegistry.java index 26574b819c..dec0552e6c 100644 --- a/src/main/java/graphql/schema/GraphQLCodeRegistry.java +++ b/src/main/java/graphql/schema/GraphQLCodeRegistry.java @@ -4,6 +4,8 @@ import graphql.Internal; import graphql.PublicApi; import graphql.schema.visibility.GraphqlFieldVisibility; +import graphql.util.flyweight.FlyweightKit; +import org.jspecify.annotations.NonNull; import java.util.HashMap; import java.util.LinkedHashMap; @@ -33,6 +35,7 @@ public class GraphQLCodeRegistry { private final Map typeResolverMap; private final GraphqlFieldVisibility fieldVisibility; private final DataFetcherFactory defaultDataFetcherFactory; + private final FlyweightKit.BiKeyMap flyweightFieldCoordinates; private GraphQLCodeRegistry(Builder builder) { this.dataFetcherMap = builder.dataFetcherMap; @@ -40,6 +43,7 @@ private GraphQLCodeRegistry(Builder builder) { this.typeResolverMap = builder.typeResolverMap; this.fieldVisibility = builder.fieldVisibility; this.defaultDataFetcherFactory = builder.defaultDataFetcherFactory; + this.flyweightFieldCoordinates = new FlyweightKit.BiKeyMap<>(); } /** @@ -58,7 +62,15 @@ public GraphqlFieldVisibility getFieldVisibility() { * @return the DataFetcher associated with this field. All fields have data fetchers */ public DataFetcher getDataFetcher(GraphQLObjectType parentType, GraphQLFieldDefinition fieldDefinition) { - return getDataFetcherImpl(FieldCoordinates.coordinates(parentType, fieldDefinition), fieldDefinition, dataFetcherMap, systemDataFetcherMap, defaultDataFetcherFactory); + return getDataFetcherImpl(flyWeightCoordinates(parentType, fieldDefinition), + fieldDefinition, + dataFetcherMap, + systemDataFetcherMap, + defaultDataFetcherFactory); + } + + private FieldCoordinates flyWeightCoordinates(@NonNull GraphQLObjectType parentType, @NonNull GraphQLFieldDefinition fieldDefinition) { + return flyweightFieldCoordinates.computeIfAbsent(parentType.getName(),fieldDefinition.getName(), FieldCoordinates::coordinates); } /** diff --git a/src/main/java/graphql/schema/PropertyFetchingImpl.java b/src/main/java/graphql/schema/PropertyFetchingImpl.java index c17ba706b6..bc07551357 100644 --- a/src/main/java/graphql/schema/PropertyFetchingImpl.java +++ b/src/main/java/graphql/schema/PropertyFetchingImpl.java @@ -4,6 +4,8 @@ import graphql.Internal; import graphql.schema.fetching.LambdaFetchingSupport; import graphql.util.StringKit; +import graphql.util.flyweight.FlyweightKit; +import org.jspecify.annotations.NonNull; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -38,6 +40,8 @@ public class PropertyFetchingImpl { private final ConcurrentMap METHOD_CACHE = new ConcurrentHashMap<>(); private final ConcurrentMap FIELD_CACHE = new ConcurrentHashMap<>(); private final ConcurrentMap NEGATIVE_CACHE = new ConcurrentHashMap<>(); + private final FlyweightKit.TriKeyMap FLYWEIGHT_CACHEKEY_CACHE = new FlyweightKit.TriKeyMap<>(); + private final Class singleArgumentType; public PropertyFetchingImpl(Class singleArgumentType) { @@ -376,6 +380,7 @@ public void clearReflectionCache() { METHOD_CACHE.clear(); FIELD_CACHE.clear(); NEGATIVE_CACHE.clear(); + FLYWEIGHT_CACHEKEY_CACHE.clear(); } public boolean setUseSetAccessible(boolean flag) { @@ -390,18 +395,18 @@ public boolean setUseNegativeCache(boolean flag) { return USE_NEGATIVE_CACHE.getAndSet(flag); } - private CacheKey mkCacheKey(Object object, String propertyName) { + private CacheKey mkCacheKey(@NonNull Object object, @NonNull String propertyName) { Class clazz = object.getClass(); ClassLoader classLoader = clazz.getClassLoader(); - return new CacheKey(classLoader, clazz.getName(), propertyName); + return FLYWEIGHT_CACHEKEY_CACHE.computeIfAbsent(classLoader, clazz.getName(), propertyName, CacheKey::new); } - private static final class CacheKey { + static final class CacheKey { private final ClassLoader classLoader; private final String className; private final String propertyName; - private CacheKey(ClassLoader classLoader, String className, String propertyName) { + CacheKey(ClassLoader classLoader, String className, String propertyName) { this.classLoader = classLoader; this.className = className; this.propertyName = propertyName; diff --git a/src/main/java/graphql/util/flyweight/FlyweightKit.java b/src/main/java/graphql/util/flyweight/FlyweightKit.java new file mode 100644 index 0000000000..13fc70b554 --- /dev/null +++ b/src/main/java/graphql/util/flyweight/FlyweightKit.java @@ -0,0 +1,79 @@ +package graphql.util.flyweight; + +import graphql.Internal; +import graphql.util.LockKit; +import org.jspecify.annotations.NullMarked; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiFunction; + +@Internal +@NullMarked +public class FlyweightKit { + + public static class BiKeyMap { + private final Map> map; + private final LockKit.ReentrantLock lock; + + + public BiKeyMap() { + map = new HashMap<>(); + lock = new LockKit.ReentrantLock(); + } + + public T computeIfAbsent(K1 key1, K2 key2, BiFunction computeFunc) { + lock.lock(); + try { + Map k2Map = map.computeIfAbsent(key1, k1 -> new HashMap<>()); + return k2Map.computeIfAbsent(key2, k2 -> computeFunc.apply(key1, k2)); + } finally { + lock.unlock(); + } + } + + public void clear() { + clearMap(map, lock); + } + } + + public interface TriFunction { + T apply(K1 k1, K2 k2, K3 k3); + } + + public static class TriKeyMap { + private final Map>> map; + private final LockKit.ReentrantLock lock; + + public TriKeyMap() { + map = new HashMap<>(); + lock = new LockKit.ReentrantLock(); + } + + public T computeIfAbsent(K1 key1, K2 key2, K3 key3, TriFunction computeFunc) { + lock.lock(); + try { + Map> k2Map = map.computeIfAbsent(key1, k1 -> new HashMap<>()); + Map k3Map = k2Map.computeIfAbsent(key2, k2 -> new HashMap<>()); + return k3Map.computeIfAbsent(key3, k3 -> computeFunc.apply(key1, key2, k3)); + } finally { + lock.unlock(); + } + } + + public void clear() { + clearMap(map, lock); + } + } + + @SuppressWarnings("rawtypes") + private static void clearMap(Map map, LockKit.ReentrantLock lock) { + lock.lock(); + try { + map.clear(); + } finally { + lock.unlock(); + } + } + +} diff --git a/src/test/groovy/graphql/util/flyweight/FlyweightKitTest.groovy b/src/test/groovy/graphql/util/flyweight/FlyweightKitTest.groovy new file mode 100644 index 0000000000..2bb3d28f13 --- /dev/null +++ b/src/test/groovy/graphql/util/flyweight/FlyweightKitTest.groovy @@ -0,0 +1,103 @@ +package graphql.util.flyweight + +import spock.lang.Specification + +class FlyweightKitTest extends Specification { + + class CacheKey { + private String className; + private String propertyName; + + CacheKey(String className, String propertyName) { + this.className = className + this.propertyName = propertyName + } + + String getClassName() { + return className + } + + String getPropertyName() { + return propertyName + } + } + + def "can compute objects with a 2 key map"() { + def flyweightMap = new FlyweightKit.BiKeyMap() + when: + def cacheKey1 = flyweightMap.computeIfAbsent("classX", "foo", { k1, k2 -> new CacheKey(k1, k2) }) + + then: + cacheKey1.getClassName() == "classX" + cacheKey1.getPropertyName() == "foo" + + when: + def cacheKey2 = flyweightMap.computeIfAbsent("classX", "foo", { k1, k2 -> new CacheKey(k1, k2) }) + + then: + cacheKey2 === cacheKey1 + + when: + def cacheKey3 = flyweightMap.computeIfAbsent("classY", "foo", { k1, k2 -> new CacheKey(k1, k2) }) + + then: + cacheKey3 !== cacheKey1 + cacheKey3.getClassName() == "classY" + cacheKey3.getPropertyName() == "foo" + + when: + def cacheKey4 = flyweightMap.computeIfAbsent("classY", "bar", { k1, k2 -> new CacheKey(k1, k2) }) + + then: + cacheKey4 !== cacheKey3 + cacheKey4.getClassName() == "classY" + cacheKey4.getPropertyName() == "bar" + + when: + def cacheKey5 = flyweightMap.computeIfAbsent("classY", "bar", { k1, k2 -> new CacheKey(k1, k2) }) + + then: + cacheKey5 === cacheKey4 + } + + def "can compute objects with a 3 key map"() { + def flyweightMap = new FlyweightKit.TriKeyMap() + + when: + def value1 = flyweightMap.computeIfAbsent("a", "x", 1, { k1, k2, k3 -> "$k1:$k2:$k3" }) + + then: + value1 == "a:x:1" + + when: + def value2 = flyweightMap.computeIfAbsent("a", "x", 1, { k1, k2, k3 -> "$k1:$k2:$k3" }) + + then: + value2 == "a:x:1" + value2 === value1 + + when: + def value3 = flyweightMap.computeIfAbsent("a", "x", 2, { k1, k2, k3 -> "$k1:$k2:$k3" }) + + then: + value3 == "a:x:2" + + when: + def value4 = flyweightMap.computeIfAbsent("b", "x", 1, { k1, k2, k3 -> "$k1:$k2:$k3" }) + + then: + value4 == "b:x:1" + + when: + def value5 = flyweightMap.computeIfAbsent("b", "x", 2, { k1, k2, k3 -> "$k1:$k2:$k3" }) + + then: + value5 == "b:x:2" + + when: + def value6 = flyweightMap.computeIfAbsent("b", "x", 2, { k1, k2, k3 -> "$k1:$k2:$k3" }) + + then: + value6 === value5 + } +}