Skip to content

Commit 358df97

Browse files
authored
Perf: memoize collectSubfields (#1130)
* Perf: memoize collectSubfields Collecting subfields occurs after resolving a field's value and before resolving subfield values. This step collects fragment spreads and checks inline fragment conditions. When fetching a list of things, this step is computed with the same inputs and expecting the same outputs for each item in the list. Memoizing ensures the work is done at most once per type returned from a list. I tested this against the introspection query (which is both synchronous and complex) against a large schema and saw a ~15%-25% reduction in runtime. In practice I don't expect most queries to see this level of speedup as most queries are limited by backend communication and not execution overhead. * Include context in memoization, Factor out memoize3
1 parent 3848c75 commit 358df97

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/execution/execute.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { GraphQLError, locatedError } from '../error';
1212
import invariant from '../jsutils/invariant';
1313
import isInvalid from '../jsutils/isInvalid';
1414
import isNullish from '../jsutils/isNullish';
15+
import memoize3 from '../jsutils/memoize3';
1516
import type { ObjMap } from '../jsutils/ObjMap';
1617
import type { MaybePromise } from '../jsutils/MaybePromise';
1718

@@ -1236,6 +1237,21 @@ function collectAndExecuteSubfields(
12361237
result: mixed,
12371238
): mixed {
12381239
// Collect sub-fields to execute to complete this value.
1240+
const subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes);
1241+
return executeFields(exeContext, returnType, result, path, subFieldNodes);
1242+
}
1243+
1244+
/**
1245+
* A memoized collection of relevant subfields in the context of the return
1246+
* type. Memoizing ensures the subfields are not repeatedly calculated, which
1247+
* saves overhead when resolving lists of values.
1248+
*/
1249+
const collectSubfields = memoize3(_collectSubfields);
1250+
function _collectSubfields(
1251+
exeContext: ExecutionContext,
1252+
returnType: GraphQLObjectType,
1253+
fieldNodes: $ReadOnlyArray<FieldNode>,
1254+
): ObjMap<Array<FieldNode>> {
12391255
let subFieldNodes = Object.create(null);
12401256
const visitedFragmentNames = Object.create(null);
12411257
for (let i = 0; i < fieldNodes.length; i++) {
@@ -1250,8 +1266,7 @@ function collectAndExecuteSubfields(
12501266
);
12511267
}
12521268
}
1253-
1254-
return executeFields(exeContext, returnType, result, path, subFieldNodes);
1269+
return subFieldNodes;
12551270
}
12561271

12571272
/**

src/jsutils/memoize3.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
/**
11+
* Memoizes the provided three-argument function.
12+
*/
13+
export default function memoize3<T: (a1: any, a2: any, a3: any) => any>(
14+
fn: T,
15+
): T {
16+
let cache0;
17+
function memoized(a1, a2, a3) {
18+
if (!cache0) {
19+
cache0 = new WeakMap();
20+
}
21+
let cache1 = cache0.get(a1);
22+
let cache2;
23+
if (cache1) {
24+
cache2 = cache1.get(a2);
25+
if (cache2) {
26+
const cachedValue = cache2.get(a3);
27+
if (cachedValue) {
28+
return cachedValue;
29+
}
30+
}
31+
} else {
32+
cache1 = new WeakMap();
33+
cache0.set(a1, cache1);
34+
}
35+
if (!cache2) {
36+
cache2 = new WeakMap();
37+
cache1.set(a2, cache2);
38+
}
39+
const newValue = fn.apply(this, arguments);
40+
cache2.set(a3, newValue);
41+
return newValue;
42+
}
43+
return (memoized: any);
44+
}

0 commit comments

Comments
 (0)