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

fix(graphcache): Remove for-of syntax from Graphcache code for JSC memory reduction #3698

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wet-buses-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': patch
---

Remove `for-of` syntax from `@urql/exchange-graphcache` for JSC memory reduction
6 changes: 3 additions & 3 deletions exchanges/graphcache/src/ast/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ export const isDeferred = (
): boolean => {
const { defer } = getDirectives(node);
if (defer) {
for (const argument of defer.arguments || []) {
if (getName(argument) === 'if') {
for (let i = 0; defer.arguments && i < defer.arguments.length; i++) {
if (getName(defer.arguments[i]) === 'if') {
// Return whether `@defer(if: )` is enabled
return !!valueFromASTUntyped(argument.value, vars);
return !!valueFromASTUntyped(defer.arguments[i].value, vars);
}
}
return true;
Expand Down
65 changes: 50 additions & 15 deletions exchanges/graphcache/src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ export const cacheExchange =
const isBlockedByOptimisticUpdate = (
dependencies: Dependencies
): boolean => {
for (const dep of dependencies.values())
if (blockedDependencies.has(dep)) return true;
const depsIterator = dependencies[Symbol.iterator]();
for (
let depsEntry = depsIterator.next();
!depsEntry.done;
depsEntry = depsIterator.next()
)
if (blockedDependencies.has(depsEntry.value)) return true;
return false;
};

Expand All @@ -103,9 +108,22 @@ export const cacheExchange =
) => {
if (dependencies) {
// Collect operations that will be updated due to cache changes
for (const dep of dependencies.values()) {
const keys = deps.get(dep);
if (keys) for (const key of keys.values()) pendingOperations.add(key);
const depsIterator = dependencies[Symbol.iterator]();
for (
let depsEntry = depsIterator.next();
!depsEntry.done;
depsEntry = depsIterator.next()
) {
const keys = deps.get(depsEntry.value);
if (keys) {
const keysIterator = keys[Symbol.iterator]();
for (
let keysEntry = keysIterator.next();
!keysEntry.done;
keysEntry = keysIterator.next()
)
pendingOperations.add(keysEntry.value);
}
}
}
};
Expand All @@ -116,15 +134,21 @@ export const cacheExchange =
isOptimistic: boolean
) => {
// Reexecute collected operations and delete them from the mapping
for (const key of pendingOperations.values()) {
if (key !== operation.key) {
const op = operations.get(key);
const pendingIterator = pendingOperations[Symbol.iterator]();
for (
let pendingEntry = pendingIterator.next();
!pendingEntry.done;
pendingEntry = pendingIterator.next()
) {
if (pendingEntry.value !== operation.key) {
const op = operations.get(pendingEntry.value);
if (op) {
// Collect all dependent operations if the reexecuting operation is a query
if (operation.kind === 'query') dependentOperations.add(key);
if (operation.kind === 'query')
dependentOperations.add(pendingEntry.value);
let policy: RequestPolicy = 'cache-first';
if (requestedRefetch.has(key)) {
requestedRefetch.delete(key);
if (requestedRefetch.has(pendingEntry.value)) {
requestedRefetch.delete(pendingEntry.value);
policy = 'cache-and-network';
}
client.reexecuteOperation(toRequestPolicy(op, policy));
Expand Down Expand Up @@ -175,7 +199,13 @@ export const cacheExchange =
clearDataState();
if (dependencies.size) {
// Update blocked optimistic dependencies
for (const dep of dependencies.values()) blockedDependencies.add(dep);
const depsIterator = dependencies[Symbol.iterator]();
for (
let depsEntry = depsIterator.next();
!depsEntry.done;
depsEntry = depsIterator.next()
)
blockedDependencies.add(depsEntry.value);
// Store optimistic dependencies for update
optimisticKeysToDependencies.set(operation.key, dependencies);
// Update related queries
Expand Down Expand Up @@ -205,9 +235,14 @@ export const cacheExchange =

// This updates the known dependencies for the passed operation
const updateDependencies = (op: Operation, dependencies: Dependencies) => {
for (const dep of dependencies.values()) {
let depOps = deps.get(dep);
if (!depOps) deps.set(dep, (depOps = new Set()));
const depsIterator = dependencies[Symbol.iterator]();
for (
let depsEntry = depsIterator.next();
!depsEntry.done;
depsEntry = depsIterator.next()
) {
let depOps = deps.get(depsEntry.value);
if (!depOps) deps.set(depsEntry.value, (depOps = new Set()));
depOps.add(op.key);
}
};
Expand Down
7 changes: 4 additions & 3 deletions exchanges/graphcache/src/operations/invalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export const invalidateType = (
excludedEntities: string[]
) => {
const types = InMemoryData.getEntitiesForType(typename);
for (const entity of types) {
if (excludedEntities.includes(entity)) continue;
invalidateEntity(entity);
const iterator = types[Symbol.iterator]();
for (let result = iterator.next(); !result.done; result = iterator.next()) {
if (excludedEntities.includes(result.value)) continue;
invalidateEntity(result.value);
}
};
48 changes: 34 additions & 14 deletions exchanges/graphcache/src/store/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,14 @@ export const gc = () => {
// Iterate over all entities that have been marked for deletion
// Entities have been marked for deletion in `updateRCForEntity` if
// their reference count dropped to 0
for (const entityKey of currentData!.gc.keys()) {
const gcIterator = currentData!.gc[Symbol.iterator]();
for (
let gcEntry = gcIterator.next();
!gcEntry.done;
gcEntry = gcIterator.next()
) {
const entityKey = gcEntry.value;

// Remove the current key from the GC batch
currentData!.gc.delete(entityKey);

Expand Down Expand Up @@ -640,22 +647,30 @@ const squashLayer = (layerKey: number) => {

const links = currentData!.links.optimistic.get(layerKey);
if (links) {
for (const entry of links.entries()) {
const entityKey = entry[0];
const keyMap = entry[1];
const linksIterator = links[Symbol.iterator]();
for (
let linksEntry = linksIterator.next();
!linksEntry.done;
linksEntry = linksIterator.next()
) {
const keyMap = linksEntry.value[1];
for (const fieldKey in keyMap) {
writeLink(entityKey, fieldKey, keyMap[fieldKey]);
writeLink(linksEntry.value[0], fieldKey, keyMap[fieldKey]);
}
}
}

const records = currentData!.records.optimistic.get(layerKey);
if (records) {
for (const entry of records.entries()) {
const entityKey = entry[0];
const keyMap = entry[1];
const recordsIterator = records[Symbol.iterator]();
for (
let recordsEntry = recordsIterator.next();
!recordsEntry.done;
recordsEntry = recordsIterator.next()
) {
const keyMap = recordsEntry.value[1];
for (const fieldKey in keyMap) {
writeRecord(entityKey, fieldKey, keyMap[fieldKey]);
writeRecord(recordsEntry.value[0], fieldKey, keyMap[fieldKey]);
}
}
}
Expand Down Expand Up @@ -683,15 +698,20 @@ export const persistData = () => {
currentOptimistic = true;
currentOperation = 'read';
const entries: SerializedEntries = {};
for (const key of currentData!.persist.keys()) {
const { entityKey, fieldKey } = deserializeKeyInfo(key);
const persistIterator = currentData!.persist[Symbol.iterator]();
for (
let persistEntry = persistIterator.next();
!persistEntry.done;
persistEntry = persistIterator.next()
) {
const { entityKey, fieldKey } = deserializeKeyInfo(persistEntry.value);
let x: void | Link | EntityField;
if ((x = readLink(entityKey, fieldKey)) !== undefined) {
entries[key] = `:${stringifyVariables(x)}`;
entries[persistEntry.value] = `:${stringifyVariables(x)}`;
} else if ((x = readRecord(entityKey, fieldKey)) !== undefined) {
entries[key] = stringifyVariables(x);
entries[persistEntry.value] = stringifyVariables(x);
} else {
entries[key] = undefined;
entries[persistEntry.value] = undefined;
}
}

Expand Down