Skip to content
Closed
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
5 changes: 5 additions & 0 deletions src/jmh/java/benchmark/BenchmarkUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package benchmark;

import org.openjdk.jmh.infra.Blackhole;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -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.");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those crazy JMH people!

}
}
119 changes: 119 additions & 0 deletions src/jmh/java/graphql/schema/FlyWeightBenchmark.java
Original file line number Diff line number Diff line change
@@ -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<String, String, FieldCoordinates> flyweightFieldCoordinates = new FlyweightKit.BiKeyMap<>();
static FlyweightKit.TriKeyMap<ClassLoader, String, String, PropertyFetchingImpl.CacheKey> 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),
() -> {
}

);
}
}
14 changes: 13 additions & 1 deletion src/main/java/graphql/schema/GraphQLCodeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,13 +35,15 @@ public class GraphQLCodeRegistry {
private final Map<String, TypeResolver> typeResolverMap;
private final GraphqlFieldVisibility fieldVisibility;
private final DataFetcherFactory<?> defaultDataFetcherFactory;
private final FlyweightKit.BiKeyMap<String, String, FieldCoordinates> flyweightFieldCoordinates;

private GraphQLCodeRegistry(Builder builder) {
this.dataFetcherMap = builder.dataFetcherMap;
this.systemDataFetcherMap = builder.systemDataFetcherMap;
this.typeResolverMap = builder.typeResolverMap;
this.fieldVisibility = builder.fieldVisibility;
this.defaultDataFetcherFactory = builder.defaultDataFetcherFactory;
this.flyweightFieldCoordinates = new FlyweightKit.BiKeyMap<>();
}

/**
Expand All @@ -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);
}

/**
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/graphql/schema/PropertyFetchingImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +40,8 @@ public class PropertyFetchingImpl {
private final ConcurrentMap<CacheKey, CachedMethod> METHOD_CACHE = new ConcurrentHashMap<>();
private final ConcurrentMap<CacheKey, Field> FIELD_CACHE = new ConcurrentHashMap<>();
private final ConcurrentMap<CacheKey, CacheKey> NEGATIVE_CACHE = new ConcurrentHashMap<>();
private final FlyweightKit.TriKeyMap<ClassLoader,String,String,CacheKey> FLYWEIGHT_CACHEKEY_CACHE = new FlyweightKit.TriKeyMap<>();

private final Class<?> singleArgumentType;

public PropertyFetchingImpl(Class<?> singleArgumentType) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/graphql/util/flyweight/FlyweightKit.java
Original file line number Diff line number Diff line change
@@ -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<K1, K2, T> {
private final Map<K1, Map<K2, T>> map;
private final LockKit.ReentrantLock lock;


public BiKeyMap() {
map = new HashMap<>();
lock = new LockKit.ReentrantLock();
}

public T computeIfAbsent(K1 key1, K2 key2, BiFunction<K1, K2, T> computeFunc) {
lock.lock();
try {
Map<K2, T> 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<K1, K2, K3, T> {
T apply(K1 k1, K2 k2, K3 k3);
}

public static class TriKeyMap<K1, K2, K3, T> {
private final Map<K1, Map<K2, Map<K3, T>>> 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<K1, K2, K3, T> computeFunc) {
lock.lock();
try {
Map<K2, Map<K3, T>> k2Map = map.computeIfAbsent(key1, k1 -> new HashMap<>());
Map<K3, T> 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();
}
}

}
Loading