Description
Issue Description
Descriptions:
Since @apollo/client
v3.9, calling client.cache.gc()
increase memory usage in various cache subsequently.
Some affected cache are:
- inMemoryCache. executeSelectionSet
- inMemoryCache. executeSubSelectedArray
- client.cache.addTypenameTransform.resultCache
This behaviour did NOT occur in v3.8 .
Also, I think there is performance impact here as well when trying to re-read previous gql query document, after calling cache.gc()
This happens because subsequent usage of cache.storeReader.executeSelectionSet (optimism.wrap) will result in cache miss.
Some other notes:
When doing git bisect on apollo-client repo, the first commit introducing this behaviour is bd26676
From reading the code, I think by reseting the cache in inMemoryCache.addTypenameTransform
(source), it produces different cache key for the same gql query subsequently.
The chain is something like this:
- cache.storeReader.executeSelectionSet#makeCacheKey uses
selectionSet
as part of its key selectionSet
is returned from QueryManager.transform()Querymanager.transform()
makes use ofinMemoryCache.addTypenameToDocument
(source)inMemoryCache.addTypenameToDocument()
returns different value for same documentNode afterinMemoryCache.addTypenameTransform.resetCache()
ininMemoryCache.gc()
Hope this make sense.
Link to Reproduction
Reproduction Steps
Reproduction steps:
- Click on "Toggle Show Location & cache.GC()" multiple times
- this button mounts/unmounts a simple React component that use
useQuery()
hooks (Code copied from get-started doc) - this button also triggers
apolloClient.cache.gc()
at the same time
- this button mounts/unmounts a simple React component that use
- Observe the various cache size keeps increasing. For example:
inMemoryCache.executeSelectionSet.size
- This behaviour started since v3.9.0
- in v3.8, the
inMemoryCache.executeSelectionSet.size
stays at6
Reproduction screenshot:
@apollo/client
version
3.12.11