Skip to content

Commit af4acdc

Browse files
authored
Fix memory leak #13036 (#13045)
* try to fix memory leak * extract `memoize` function * changeset * byte shaving * Clean up Prettier, Size-limit, and Api-Extractor --------- Co-authored-by: phryneas <[email protected]>
1 parent a18037b commit af4acdc

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

.changeset/long-cats-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Fix memory leak #13036

.size-limits.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 43931,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38761,
4-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33451,
5-
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27513
2+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (CJS)": 44009,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production) (CJS)": 38803,
4+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\"": 33512,
5+
"import { ApolloClient, InMemoryCache, HttpLink } from \"@apollo/client\" (production)": 27576
66
}

src/utilities/internal/checkDocument.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Checks the document for errors and throws an exception if there is an error.
22

3-
import { WeakCache } from "@wry/caches";
43
import type { ASTNode } from "graphql";
54
import type { DocumentNode, OperationTypeNode } from "graphql";
65
import { Kind, visit } from "graphql";
7-
import { wrap } from "optimism";
86

97
import { __DEV__ } from "@apollo/client/utilities/environment";
108
import {
@@ -16,6 +14,7 @@ import { defaultCacheSizes } from "../../utilities/caching/sizes.js";
1614
import { cacheSizes } from "../caching/sizes.js";
1715

1816
import { getOperationName } from "./getOperationName.js";
17+
import { memoize } from "./memoize.js";
1918

2019
/**
2120
* Checks the document for errors and throws an exception if there is an error.
@@ -25,7 +24,7 @@ import { getOperationName } from "./getOperationName.js";
2524
export const checkDocument: (
2625
doc: DocumentNode,
2726
expectedType?: OperationTypeNode
28-
) => void = wrap(
27+
) => void = memoize(
2928
(doc: DocumentNode, expectedType?: OperationTypeNode): void => {
3029
invariant(
3130
doc && doc.kind === "Document",
@@ -97,6 +96,5 @@ string in a "gql" tag? http://docs.apollostack.com/apollo-client/core.html#gql`
9796
},
9897
{
9998
max: cacheSizes["checkDocument"] || defaultCacheSizes["checkDocument"],
100-
cache: WeakCache,
10199
}
102100
);

src/utilities/internal/memoize.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Trie } from "@wry/trie";
2+
3+
import { AutoCleanedWeakCache } from "./caches.js";
4+
5+
/**
6+
* Naive alternative to `wrap` without any dependency tracking, potentially avoiding resulting memory leaks.
7+
*/
8+
export function memoize<TArgs extends any[], TResult>(
9+
fn: (...args: TArgs) => TResult,
10+
{ max }: { max: number }
11+
): (...args: TArgs) => TResult {
12+
const keys = new Trie<{}>(true);
13+
const cache = new AutoCleanedWeakCache<
14+
{},
15+
{ result?: TResult; error?: unknown }
16+
>(max);
17+
18+
return (...args: TArgs): TResult => {
19+
const cacheKey = keys.lookupArray(args);
20+
const cached = cache.get(cacheKey);
21+
if (cached) {
22+
if (cached.error) {
23+
throw cached.error;
24+
}
25+
return cached.result!;
26+
}
27+
28+
const entry = cache.set(cacheKey, {});
29+
try {
30+
return (entry.result = fn(...args));
31+
} catch (error) {
32+
entry.error = error;
33+
throw error;
34+
}
35+
};
36+
}

0 commit comments

Comments
 (0)