Skip to content

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

Verified

This commit was signed with the committer’s verified signature.
benjamn Ben Newman
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!

hwillson added 2 commits June 6, 2018 18:10

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@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.

None yet

3 participants