-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Caching parse and validate by default #4121
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
Open
bbakerman
wants to merge
8
commits into
master
Choose a base branch
from
caching-query-parsing-and-validation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b2f6242
Caching parse and validate by default
bbakerman 4225518
Added bench mark
bbakerman daa4663
Added locale to cache key
bbakerman fe77fd8
Tweaked benchmark
bbakerman 02299f3
More code tweaks and javadoc
bbakerman adea9a6
Added option in graphql to turn off document caching
bbakerman 1e3785a
Made graphql.execution.preparsed.NoOpPreparsedDocumentProvider api
bbakerman c42baa7
Made the cache lookup be either async or not
bbakerman 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,83 @@ | ||
| package benchmark; | ||
|
|
||
| import graphql.GraphQL; | ||
| import graphql.StarWarsSchema; | ||
| import graphql.execution.preparsed.NoOpPreparsedDocumentProvider; | ||
| import graphql.execution.preparsed.PreparsedDocumentProvider; | ||
| import graphql.execution.preparsed.caching.CachingDocumentProvider; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| 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; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @Warmup(iterations = 2, time = 5) | ||
| @Measurement(iterations = 3, time = 2) | ||
| @Fork(2) | ||
| public class CachingDocumentBenchmark { | ||
|
|
||
| @Param({"50", "500", "5000"}) | ||
| public int querySize; | ||
|
|
||
| @Param({"10", "50", "500"}) | ||
| public int queryCount; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setUp() { | ||
| } | ||
|
|
||
| private static final GraphQL GRAPHQL_CACHING_ON = buildGraphQL(true); | ||
| private static final GraphQL GRAPHQL_CACHING_OFF = buildGraphQL(false); | ||
|
|
||
| @Benchmark | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| public void benchMarkCachingOnAvgTime() { | ||
| executeQuery(true); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| public void benchMarkCachingOffAvgTime() { | ||
| executeQuery(false); | ||
| } | ||
|
|
||
| public void executeQuery(boolean cachingOn) { | ||
| String query = buildQuery(querySize); | ||
| GraphQL graphQL = cachingOn ? GRAPHQL_CACHING_ON : GRAPHQL_CACHING_OFF; | ||
|
|
||
| for (int i = 0; i < queryCount; i++) { | ||
| graphQL.execute(query); | ||
| } | ||
| } | ||
|
|
||
| private static String buildQuery(int howManyAliases) { | ||
| StringBuilder query = new StringBuilder("query q { hero { \n"); | ||
| for (int i = 0; i < howManyAliases; i++) { | ||
| query.append("nameAlias").append(i).append(" : name\n"); | ||
| } | ||
| query.append("}}"); | ||
| return query.toString(); | ||
| } | ||
|
|
||
| private static GraphQL buildGraphQL(boolean cachingOn) { | ||
| PreparsedDocumentProvider documentProvider = NoOpPreparsedDocumentProvider.INSTANCE; | ||
| if (cachingOn) { | ||
| documentProvider = new CachingDocumentProvider(); | ||
| } | ||
| return GraphQL.newGraphQL(StarWarsSchema.starWarsSchema) | ||
| .preparsedDocumentProvider(documentProvider) | ||
| .build(); | ||
| } | ||
| } |
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
68 changes: 68 additions & 0 deletions
68
src/main/java/graphql/execution/preparsed/caching/CachingDocumentProvider.java
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,68 @@ | ||
| package graphql.execution.preparsed.caching; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import graphql.ExecutionInput; | ||
| import graphql.PublicApi; | ||
| import graphql.execution.preparsed.PreparsedDocumentEntry; | ||
| import graphql.execution.preparsed.PreparsedDocumentProvider; | ||
| import org.jspecify.annotations.NullMarked; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Function; | ||
|
|
||
| import static graphql.execution.Async.toCompletableFuture; | ||
|
|
||
| /** | ||
| * The CachingDocumentProvider allows previously parsed and validated operations to be cached and | ||
| * hence re-used. This can lead to significant time savings, especially for large operations. | ||
| * <p> | ||
| * By default, graphql-java will cache the parsed {@link PreparsedDocumentEntry} that represents | ||
| * a parsed and validated graphql query IF {@link Caffeine} is present on the class path | ||
| * at runtime. If it's not then no caching takes place. | ||
| * <p> | ||
| * You can provide your own {@link DocumentCache} implementation and hence use any cache | ||
| * technology you like. | ||
| */ | ||
| @PublicApi | ||
| @NullMarked | ||
| public class CachingDocumentProvider implements PreparsedDocumentProvider { | ||
| private final DocumentCache documentCache; | ||
|
|
||
| /** | ||
| * By default, it will try to use a {@link Caffeine} backed implementation if it's on the class | ||
| * path otherwise it will become a non caching mechanism. | ||
| * | ||
| * @see CaffeineDocumentCache | ||
| */ | ||
| public CachingDocumentProvider() { | ||
| this(new CaffeineDocumentCache()); | ||
| } | ||
|
|
||
| /** | ||
| * You can use your own cache implementation and provide that to this class to use | ||
| * | ||
| * @param documentCache the cache to use | ||
| */ | ||
| public CachingDocumentProvider(DocumentCache documentCache) { | ||
| this.documentCache = documentCache; | ||
| } | ||
|
|
||
| /** | ||
| * @return the {@link DocumentCache} being used | ||
| */ | ||
| public DocumentCache getDocumentCache() { | ||
| return documentCache; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<PreparsedDocumentEntry> getDocumentAsync(ExecutionInput executionInput, Function<ExecutionInput, PreparsedDocumentEntry> parseAndValidateFunction) { | ||
| if (documentCache.isNoop()) { | ||
| // saves creating keys and doing a lookup that will just call this function anyway | ||
| return toCompletableFuture(parseAndValidateFunction.apply(executionInput)); | ||
| } | ||
| DocumentCache.DocumentCacheKey cacheKey = new DocumentCache.DocumentCacheKey(executionInput.getQuery(), executionInput.getOperationName(), executionInput.getLocale()); | ||
| Object cacheEntry = documentCache.get(cacheKey, key -> parseAndValidateFunction.apply(executionInput)); | ||
| return toCompletableFuture(cacheEntry); | ||
| } | ||
|
|
||
| } | ||
77 changes: 77 additions & 0 deletions
77
src/main/java/graphql/execution/preparsed/caching/CaffeineDocumentCache.java
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,77 @@ | ||
| package graphql.execution.preparsed.caching; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import graphql.PublicApi; | ||
| import graphql.execution.preparsed.PreparsedDocumentEntry; | ||
| import graphql.util.ClassKit; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| @PublicApi | ||
| @NullMarked | ||
| public class CaffeineDocumentCache implements DocumentCache { | ||
|
|
||
| private final static boolean isCaffeineAvailable = ClassKit.isClassAvailable("com.github.benmanes.caffeine.cache.Caffeine"); | ||
|
|
||
| @Nullable | ||
| private final Object caffeineCacheObj; | ||
|
|
||
| CaffeineDocumentCache(boolean isCaffeineAvailable) { | ||
| if (isCaffeineAvailable) { | ||
| CaffeineDocumentCacheOptions options = CaffeineDocumentCacheOptions.getDefaultJvmOptions(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notice it never loads an Caffeine code unless Caffeine is on the class path |
||
| caffeineCacheObj = Caffeine.newBuilder() | ||
| .expireAfterAccess(options.getExpireAfterAccess()) | ||
| .maximumSize(options.getMaxSize()) | ||
| .build(); | ||
| } else { | ||
| caffeineCacheObj = null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a cache that works if Caffeine is on the class path otherwise its | ||
| * a no op. | ||
| */ | ||
| public CaffeineDocumentCache() { | ||
| this(isCaffeineAvailable); | ||
| } | ||
|
|
||
| /** | ||
| * If you want to control the {@link Caffeine} configuration, using this constructor and pass in your own {@link Caffeine} cache | ||
| * | ||
| * @param caffeineCache the custom {@link Caffeine} cache to use | ||
| */ | ||
| public CaffeineDocumentCache(Cache<DocumentCache.DocumentCacheKey, PreparsedDocumentEntry> caffeineCache) { | ||
| this.caffeineCacheObj = caffeineCache; | ||
| } | ||
|
|
||
| @Override | ||
| public PreparsedDocumentEntry get(DocumentCacheKey key, Function<DocumentCacheKey, PreparsedDocumentEntry> mappingFunction) { | ||
| if (isNoop()) { | ||
| return mappingFunction.apply(key); | ||
| } | ||
| return cache().get(key, mappingFunction); | ||
| } | ||
|
|
||
| private Cache<DocumentCache.DocumentCacheKey, PreparsedDocumentEntry> cache() { | ||
| //noinspection unchecked | ||
| return (Cache<DocumentCacheKey, PreparsedDocumentEntry>) requireNonNull(caffeineCacheObj); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isNoop() { | ||
| return caffeineCacheObj == null; | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidateAll() { | ||
| if (!isNoop()) { | ||
| cache().invalidateAll(); | ||
| } | ||
| } | ||
| } | ||
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.
Its surprisingly little code