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

Cache query documents transformed by InMemoryCache. #3553

Merged
merged 3 commits into from
Jun 6, 2018

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Jun 6, 2018

After #3444 removed Map-based caching for addTypenameToDocument (in order to fix memory leaks), the InMemoryCache#transformDocument method now creates a completely new DocumentNode every time it's called (assuming this.addTypename is true, which it is by default).

This commit uses a WeakMap to cache calls to addTypenameToDocument in InMemoryCache#transformDocument, so that repeated cache reads will no longer create an unbounded number of new DocumentNode objects. The benefit of the WeakMap is that it does not prevent its keys (the original DocumentNode objects) from being garbage collected, which is another way of preventing memory leaks. Note that WeakMap may have to be polyfilled in older browsers, but there are many options for that.

This optimization will be important for #3394, since the query document is involved in cache keys used to store cache partial query results.

cc @hwillson @jbaxleyiii @brunorzn

@benjamn benjamn self-assigned this Jun 6, 2018
@benjamn benjamn requested a review from hwillson June 6, 2018 22:00
After #3444 removed `Map`-based caching for `addTypenameToDocument` (in
order to fix memory leaks), the `InMemoryCache#transformDocument` method
now creates a completely new `DocumentNode` every time it's called
(assuming this.addTypename is true, which it is by default).

This commit uses a `WeakMap` to cache calls to `addTypenameToDocument` in
`InMemoryCache#transformDocument`, so that repeated cache reads will no
longer create an unbounded number of new `DocumentNode` objects. The
benefit of the `WeakMap` is that it does not prevent its keys (the
original `DocumentNode` objects) from being garbage collected, which is
another way of preventing memory leaks.  Note that `WeakMap` may have to
be polyfilled in older browsers, but there are many options for that.

This optimization will be important for #3394, since the query document is
involved in cache keys used to store cache partial query results.

cc @hwillson @jbaxleyiii @brunorzn
@benjamn benjamn force-pushed the cache-transformed-query-documents branch from 9073936 to c3382d8 Compare June 6, 2018 22:01
@apollo-cla
Copy link

apollo-cla commented Jun 6, 2018

Warnings
⚠️

There are library changes, but not tests. That's OK as long as you're refactoring existing code

Generated by 🚫 dangerJS

Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

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

Looks great @benjamn - LGTM!

@benjamn benjamn merged commit 0acfe01 into master Jun 6, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants