Repository navigation
Flyweight support of key re-used objects #3940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
| () -> { | ||
| } | ||
|
|
||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those crazy JMH people!