Skip to content

Commit

Permalink
parse function: improve memory management (#11370)
Browse files Browse the repository at this point in the history
Co-authored-by: phryneas <phryneas@users.noreply.github.com>
  • Loading branch information
phryneas and phryneas authored Nov 29, 2023
1 parent 30d17bf commit 25e2cb4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .api-reports/api-report-react.md
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,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 @@ -1931,6 +1931,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
2 changes: 1 addition & 1 deletion .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 38603,
"dist/apollo-client.min.cjs": 38625,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32203
}
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

0 comments on commit 25e2cb4

Please sign in to comment.