Skip to content

Commit c3cb401

Browse files
committed
introduce compiledJinjaCache
1 parent 46565b6 commit c3cb401

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) {
@@ -706,17 +710,28 @@ export class DataSchemaCompiler {
706710
errorsReport: ErrorReporter,
707711
options: TranspileOptions
708712
): Promise<(FileContent | undefined)> {
709-
const renderedFile = await this.yamlCompiler.renderTemplate(
710-
file,
711-
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
712-
this.pythonContext!
713-
);
713+
const cacheKey = crypto.createHash('md5').update(JSON.stringify(file.content)).digest('hex');
714+
715+
let renderedFileContent: string;
716+
717+
if (this.compiledJinjaCache.has(cacheKey)) {
718+
renderedFileContent = this.compiledJinjaCache.get(cacheKey)!;
719+
} else {
720+
const renderedFile = await this.yamlCompiler.renderTemplate(
721+
file,
722+
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
723+
this.pythonContext!
724+
);
725+
renderedFileContent = renderedFile.content;
726+
727+
this.compiledJinjaCache.set(cacheKey, renderedFileContent);
728+
}
714729

715730
// We update the jinja/yaml file content to the rendered content
716731
// to reuse the same transpileYamlFile() flow afterward which
717732
// will update the content to the transpiled js content
718733
// avoiding costly YAML/Python parsing again.
719-
file.content = renderedFile.content;
734+
file.content = renderedFileContent;
720735

721736
return this.transpileYamlFile(file, errorsReport, options);
722737
}

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)