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

parse function: improve memory management #11370

Merged
merged 13 commits into from
Nov 29, 2023
6 changes: 6 additions & 0 deletions .api-reports/api-report-react.md
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,12 @@ type OperationVariables = Record<string, any>;
// @public (undocumented)
export function parser(document: DocumentNode): IDocumentDefinition;

// @public (undocumented)
export namespace parser {
var // (undocumented)
resetCache: () => void;
}

// @public (undocumented)
type Path = ReadonlyArray<string | number>;

Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report-react_parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export function operationName(type: DocumentType_2): string;
// @public (undocumented)
export function parser(document: DocumentNode): IDocumentDefinition;

// @public (undocumented)
export namespace parser {
var // (undocumented)
resetCache: () => void;
}

// @public (undocumented)
export function verifyDocumentType(document: DocumentNode, type: DocumentType_2): void;

Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,12 @@ export function parseAndCheckHttpResponse(operations: Operation | Operation[]):
// @public (undocumented)
export function parser(document: DocumentNode): IDocumentDefinition;

// @public (undocumented)
export namespace parser {
var // (undocumented)
resetCache: () => void;
}

// @public (undocumented)
export type Path = ReadonlyArray<string | number>;

Expand Down
8 changes: 8 additions & 0 deletions .changeset/cold-llamas-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@apollo/client": patch
---

`parse` function: improve memory management
* use LRU `WeakCache` instead of `Map` to keep a limited number of parsed results
* cache is initiated lazily, only when needed
* expose `parse.resetCache()` method
5 changes: 5 additions & 0 deletions .changeset/polite-avocados-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

`print`: use `WeakCache` instead of `WeakMap`
1 change: 1 addition & 0 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const checks = [
"react",
"react-dom",
"@graphql-typed-document-node/core",
"@wry/caches",
"@wry/context",
"@wry/equality",
"@wry/trie",
Expand Down
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 38164,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32188
"dist/apollo-client.min.cjs": 38201,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32206
}
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
},
"dependencies": {
"@graphql-typed-document-node/core": "^3.1.1",
"@wry/caches": "^1.0.0",
"@wry/context": "^0.7.3",
"@wry/equality": "^0.5.6",
"@wry/trie": "^0.4.3",
Expand Down
20 changes: 19 additions & 1 deletion src/react/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WeakCache } from "@wry/caches";
import { invariant } from "../../utilities/globals/index.js";

import type {
Expand All @@ -19,7 +20,16 @@ export interface IDocumentDefinition {
variables: ReadonlyArray<VariableDefinitionNode>;
}

const cache = new Map();
let cache:
| undefined
| WeakCache<
DocumentNode,
{
name: string;
type: DocumentType;
variables: readonly VariableDefinitionNode[];
}
>;

export function operationName(type: DocumentType) {
let name;
Expand All @@ -39,6 +49,10 @@ export function operationName(type: DocumentType) {

// This parser is mostly used to safety check incoming documents.
export function parser(document: DocumentNode): IDocumentDefinition {
if (!cache) {
cache =
new WeakCache(/** TODO: decide on a maximum size (will do all max sizes in a combined separate PR) */);
}
const cached = cache.get(document);
if (cached) return cached;

Expand Down Expand Up @@ -131,6 +145,10 @@ export function parser(document: DocumentNode): IDocumentDefinition {
return payload;
}

parser.resetCache = () => {
cache = undefined;
};

export function verifyDocumentType(document: DocumentNode, type: DocumentType) {
const operation = parser(document);
const requiredOperationName = operationName(type);
Expand Down
16 changes: 8 additions & 8 deletions src/utilities/graphql/print.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type { ASTNode } from "graphql";
import { print as origPrint } from "graphql";
import { canUseWeakMap } from "../common/canUse.js";
import { WeakCache } from "@wry/caches";

let printCache: undefined | WeakMap<ASTNode, string>;
// further TODO: replace with `optimism` with a `WeakCache` once those are available
let printCache!: WeakCache<ASTNode, string>;
export const print = Object.assign(
(ast: ASTNode) => {
let result;
result = printCache?.get(ast);
let result = printCache.get(ast);

if (!result) {
result = origPrint(ast);
printCache?.set(ast, result);
printCache.set(ast, (result = origPrint(ast)));
}
return result;
},
{
reset() {
printCache = canUseWeakMap ? new WeakMap() : undefined;
printCache = new WeakCache<
ASTNode,
string
>(/** TODO: decide on a maximum size (will do all max sizes in a combined separate PR) */);
},
}
);
Expand Down
Loading