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

Various cache read and write performance optimizations. #5948

Merged
merged 11 commits into from
Feb 16, 2020
Merged
Prev Previous commit
Next Next commit
Track policies.rootIdsByTypename as well as policies.rootTypenamesById.
Believe it or not, iterating over the values of policies.rootTypenamesById
was noticeably expensive according to Chrome devtools profiling. Since
this information almost never changes, we might as well maintain it in the
format that's most convenient.
  • Loading branch information
benjamn committed Feb 15, 2020
commit 0c6ee74e14dc71b8ba651f4d949a8542632ca406
19 changes: 10 additions & 9 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,8 @@ export class Policies {
};
} = Object.create(null);

public readonly rootTypenamesById: Readonly<Record<string, string>> = {
__proto__: null, // Equivalent to Object.create(null)
ROOT_QUERY: "Query",
ROOT_MUTATION: "Mutation",
ROOT_SUBSCRIPTION: "Subscription",
};
public readonly rootIdsByTypename: Record<string, string> = Object.create(null);
public readonly rootTypenamesById: Record<string, string> = Object.create(null);

public readonly usingPossibleTypes = false;

Expand All @@ -231,6 +227,10 @@ export class Policies {
...config,
};

this.setRootTypename("Query");
this.setRootTypename("Mutation");
this.setRootTypename("Subscription");

if (config.possibleTypes) {
this.addPossibleTypes(config.possibleTypes);
}
Expand Down Expand Up @@ -346,13 +346,14 @@ export class Policies {

private setRootTypename(
which: "Query" | "Mutation" | "Subscription",
typename: string,
typename: string = which,
) {
const rootId = "ROOT_" + which.toUpperCase();
const old = this.rootTypenamesById[rootId];
if (typename !== old) {
invariant(old === which, `Cannot change root ${which} __typename more than once`);
(this.rootTypenamesById as any)[rootId] = typename;
invariant(!old || old === which, `Cannot change root ${which} __typename more than once`);
this.rootIdsByTypename[typename] = rootId;
this.rootTypenamesById[rootId] = typename;
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ export class StoreReader {

if (this.config.addTypename &&
typeof typename === "string" &&
Object.values(
policies.rootTypenamesById
).indexOf(typename) < 0) {
!policies.rootIdsByTypename[typename]) {
// Ensure we always include a default value for the __typename
// field, if we have one, and this.config.addTypename is true. Note
// that this field can be overridden by other merged objects.
Expand Down