Skip to content
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

[Draft] AST based cache #5093

Closed
wants to merge 8 commits into from
Closed

[Draft] AST based cache #5093

wants to merge 8 commits into from

Conversation

martinbonnin
Copy link
Contributor

Add an alternate cache that reads the selections from operation.document instead of using codegen and CompiledField.

This trades codegen for runtime parsing. Current thoughts:

  1. addTypename needs to be "always" if we don't want to codegen the full type definitions. This is a sensible default when using the cache anyways because a given object type might be accessed from an interface.
  2. because GQLField has no type (again because we don't want to codegen the full type definitions), it's impossible to tell if a JSON object is scalar or not, which means scalar encoded directly in the JSON will now create intermediate records. Not 100% sure how much of a problem that is.
  3. CompiledField is a public API in cacheKeyGenerator. If some people rely on rawType() this is going to be a problem. On the other hand I think using rawType() is a mistake in most cases as what's included in the cache key is __typename instead.
  4. TODO: put back keyFields and paginationArgs support.
  5. TODO: migrate data builders to use GQLField instead of CompiledField
  6. TODO: add macrobenchmarks

Current microbenchmarks (query1 is using CompiledField, query2 is using AST):

26,353,732   ns      183675 allocs    trace   .query1Sql
31,018,269   ns      261107 allocs    trace   .query2Sql
4,107,074   ns      115242 allocs    trace   .query1Memory
6,154,897   ns      185074 allocs    trace   .query2Memory

While it's slower, microbenchmarks are not very representative because it parses the document every loop while the CompiledField is built only once during classloading and kept in memory after that.

@netlify
Copy link

netlify bot commented Jul 12, 2023

Deploy Preview for apollo-android-docs canceled.

Name Link
🔨 Latest commit 75c8d52
🔍 Latest deploy log https://app.netlify.com/sites/apollo-android-docs/deploys/64aefc6046f3bb0008c3c846

@martinbonnin martinbonnin deleted the ast-cache branch July 24, 2023 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant