Skip to content

Commit

Permalink
Performance optimizations in schema and documents loading (shared pro…
Browse files Browse the repository at this point in the history
…mises) (#7344)

Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com>
  • Loading branch information
tvvignesh and kamilkisiela committed Jan 19, 2022
1 parent 4d99b10 commit 4c42e2a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-ants-search.md
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': minor
---

Performance optimizations in schema and documents loading (shared promises)
47 changes: 44 additions & 3 deletions packages/graphql-codegen-cli/src/codegen.ts
Expand Up @@ -33,6 +33,24 @@ const makeDefaultLoader = (from: string) => {
};
};

// TODO: Replace any with types
function createCache<T>(loader: (key: string) => Promise<T>) {
const cache = new Map<string, Promise<T>>();

return {
load(key: string): Promise<T> {
if (cache.has(key)) {
return cache.get(key);
}

const value = loader(key);

cache.set(key, value);
return value;
},
};
}

export async function executeCodegen(input: CodegenContext | Types.Config): Promise<Types.FileOutput[]> {
function wrapTask(task: () => void | Promise<void>, source: string) {
return async () => {
Expand Down Expand Up @@ -84,6 +102,22 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
let rootDocuments: Types.OperationDocument[];
const generates: { [filename: string]: Types.ConfiguredOutput } = {};

const schemaLoadingCache = createCache(async function (hash) {
const outputSchemaAst = await context.loadSchema(JSON.parse(hash));
const outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
return {
outputSchemaAst: outputSchemaAst,
outputSchema: outputSchema,
};
});

const documentsLoadingCache = createCache(async function (hash) {
const documents = await context.loadDocuments(JSON.parse(hash));
return {
documents: documents,
};
});

async function normalize() {
/* Load Require extensions */
const requireExtensions = normalizeInstanceOrArray<string>(config.require);
Expand Down Expand Up @@ -210,8 +244,12 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
Object.assign(schemaPointerMap, unnormalizedPtr);
}
}
outputSchemaAst = await context.loadSchema(schemaPointerMap);
outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);

const hash = JSON.stringify(schemaPointerMap);
const result = await schemaLoadingCache.load(hash);

outputSchemaAst = await result.outputSchemaAst;
outputSchema = result.outputSchema;
}, filename),
},
{
Expand All @@ -220,7 +258,10 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
debugLog(`[CLI] Loading Documents`);

const allDocuments = [...rootDocuments, ...outputSpecificDocuments];
const documents = await context.loadDocuments(allDocuments);

const hash = JSON.stringify(allDocuments);
const result = await documentsLoadingCache.load(hash);
const documents: Types.DocumentFile[] = result.documents;

if (documents.length > 0) {
outputDocuments.push(...documents);
Expand Down

1 comment on commit 4c42e2a

@vercel
Copy link

@vercel vercel bot commented on 4c42e2a Jan 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.