Skip to content

Commit

Permalink
perf(gatsby): reuse rootNode & trackedRootNodes caches across instanc…
Browse files Browse the repository at this point in the history
…es of graphqlRunner (#33695)

Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
Co-authored-by: Ward Peeters <ward@coding-tech.com>
Co-authored-by: Khaled Garbaya <kgarbaya@gmail.com>
  • Loading branch information
4 people committed Jan 11, 2022
1 parent f1dfedc commit 26882f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/gatsby/src/query/graphql-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import { IGraphQLRunnerStatResults, IGraphQLRunnerStats } from "./types"
import { IGraphQLTelemetryRecord } from "../schema/type-definitions"
import GraphQLSpanTracer from "./graphql-span-tracer"

// Preserve these caches across graphql instances.
const _rootNodeMap = new WeakMap()
const _trackedRootNodes = new WeakSet()

type Query = string | Source

export interface IQueryOptions {
Expand Down Expand Up @@ -62,6 +66,8 @@ export class GraphQLRunner {
schema,
schemaComposer: schemaCustomization.composer,
createPageDependency,
_rootNodeMap,
_trackedRootNodes,
})
this.schema = schema
this.parseCache = new Map()
Expand Down
13 changes: 9 additions & 4 deletions packages/gatsby/src/schema/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ export interface NodeModel {
}

class LocalNodeModel {
constructor({ schema, schemaComposer, createPageDependency }) {
constructor({
schema,
schemaComposer,
createPageDependency,
_rootNodeMap,
_trackedRootNodes,
}) {
this.schema = schema
this.schemaComposer = schemaComposer
this.createPageDependencyActionCreator = createPageDependency

this._rootNodeMap = new WeakMap()
this._trackedRootNodes = new WeakSet()
this._rootNodeMap = _rootNodeMap || new WeakMap()
this._trackedRootNodes = _trackedRootNodes || new WeakSet()
this._prepareNodesQueues = {}
this._prepareNodesPromises = {}
this._preparedNodesCache = new Map()
Expand Down

0 comments on commit 26882f3

Please sign in to comment.