Skip to content

Commit 22ce0c5

Browse files
committed
introduce compiledJinjaCache
1 parent db422d0 commit 22ce0c5

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

packages/cubejs-schema-compiler/src/compiler/DataSchemaCompiler.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export type DataSchemaCompilerOptions = {
7171
allowNodeRequire?: boolean;
7272
compiledScriptCache: LRUCache<string, vm.Script>;
7373
compiledYamlCache: LRUCache<string, string>;
74+
compiledJinjaCache: LRUCache<string, string>;
7475
};
7576

7677
export type TranspileOptions = {
@@ -148,6 +149,8 @@ export class DataSchemaCompiler {
148149

149150
private readonly compiledYamlCache: LRUCache<string, string>;
150151

152+
private readonly compiledJinjaCache: LRUCache<string, string>;
153+
151154
private compileV8ContextCache: vm.Context | null = null;
152155

153156
// FIXME: Is public only because of tests, should be private
@@ -182,6 +185,7 @@ export class DataSchemaCompiler {
182185
this.compilerId = options.compilerId || 'default';
183186
this.compiledScriptCache = options.compiledScriptCache;
184187
this.compiledYamlCache = options.compiledYamlCache;
188+
this.compiledJinjaCache = options.compiledJinjaCache;
185189
}
186190

187191
public compileObjects(compileServices: CompilerInterface[], objects, errorsReport: ErrorReporter) {
@@ -710,17 +714,28 @@ export class DataSchemaCompiler {
710714
errorsReport: ErrorReporter,
711715
options: TranspileOptions
712716
): Promise<(FileContent | undefined)> {
713-
const renderedFile = await this.yamlCompiler.renderTemplate(
714-
file,
715-
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
716-
this.pythonContext!
717-
);
717+
const cacheKey = crypto.createHash('md5').update(JSON.stringify(file.content)).digest('hex');
718+
719+
let renderedFileContent: string;
720+
721+
if (this.compiledJinjaCache.has(cacheKey)) {
722+
renderedFileContent = this.compiledJinjaCache.get(cacheKey)!;
723+
} else {
724+
const renderedFile = await this.yamlCompiler.renderTemplate(
725+
file,
726+
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
727+
this.pythonContext!
728+
);
729+
renderedFileContent = renderedFile.content;
730+
731+
this.compiledJinjaCache.set(cacheKey, renderedFileContent);
732+
}
718733

719734
// We update the jinja/yaml file content to the rendered content
720735
// to reuse the same transpileYamlFile() flow afterward which
721736
// will update the content to the transpiled js content
722737
// avoiding costly YAML/Python parsing again.
723-
file.content = renderedFile.content;
738+
file.content = renderedFileContent;
724739

725740
return this.transpileYamlFile(file, errorsReport, options);
726741
}

packages/cubejs-schema-compiler/src/compiler/PrepareCompiler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type PrepareCompilerOptions = {
3838
adapter?: string;
3939
compiledScriptCache?: LRUCache<string, vm.Script>;
4040
compiledYamlCache?: LRUCache<string, string>;
41+
compiledJinjaCache?: LRUCache<string, string>;
4142
};
4243

4344
export interface CompilerInterface {
@@ -61,6 +62,7 @@ export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareComp
6162

6263
const compiledScriptCache = options.compiledScriptCache || new LRUCache<string, vm.Script>({ max: 250 });
6364
const compiledYamlCache = options.compiledYamlCache || new LRUCache<string, string>({ max: 250 });
65+
const compiledJinjaCache = options.compiledJinjaCache || new LRUCache<string, string>({ max: 250 });
6466

6567
const transpilers: TranspilerInterface[] = [
6668
new ValidationTranspiler(),
@@ -82,6 +84,7 @@ export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareComp
8284
viewCompilationGate,
8385
compiledScriptCache,
8486
compiledYamlCache,
87+
compiledJinjaCache,
8588
viewCompilers: [viewCompiler],
8689
cubeCompilers: [cubeEvaluator, joinGraph, metaTransformer],
8790
contextCompilers: [contextEvaluator],

0 commit comments

Comments
 (0)