Skip to content

Commit bef0381

Browse files
committed
introduce compiledJinjaCache
1 parent ea56261 commit bef0381

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
@@ -89,6 +89,7 @@ export type DataSchemaCompilerOptions = {
8989
allowNodeRequire?: boolean;
9090
compiledScriptCache: LRUCache<string, vm.Script>;
9191
compiledYamlCache: LRUCache<string, string>;
92+
compiledJinjaCache: LRUCache<string, string>;
9293
};
9394

9495
export type TranspileOptions = {
@@ -166,6 +167,8 @@ export class DataSchemaCompiler {
166167

167168
private readonly compiledYamlCache: LRUCache<string, string>;
168169

170+
private readonly compiledJinjaCache: LRUCache<string, string>;
171+
169172
private compileV8ContextCache: vm.Context | null = null;
170173

171174
// FIXME: Is public only because of tests, should be private
@@ -200,6 +203,7 @@ export class DataSchemaCompiler {
200203
this.compilerId = options.compilerId || 'default';
201204
this.compiledScriptCache = options.compiledScriptCache;
202205
this.compiledYamlCache = options.compiledYamlCache;
206+
this.compiledJinjaCache = options.compiledJinjaCache;
203207
}
204208

205209
public compileObjects(compileServices: CompilerInterface[], objects, errorsReport: ErrorReporter) {
@@ -765,13 +769,24 @@ export class DataSchemaCompiler {
765769
errorsReport: ErrorReporter,
766770
options: TranspileOptions
767771
): Promise<(FileContent | undefined)> {
768-
const renderedFile = await this.yamlCompiler.renderTemplate(
769-
file,
770-
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
771-
this.pythonContext!
772-
);
772+
const cacheKey = crypto.createHash('md5').update(JSON.stringify(file.content)).digest('hex');
773+
774+
let renderedFileContent: string;
775+
776+
if (this.compiledJinjaCache.has(cacheKey)) {
777+
renderedFileContent = this.compiledJinjaCache.get(cacheKey)!;
778+
} else {
779+
const renderedFile = await this.yamlCompiler.renderTemplate(
780+
file,
781+
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
782+
this.pythonContext!
783+
);
784+
renderedFileContent = renderedFile.content;
785+
786+
this.compiledJinjaCache.set(cacheKey, renderedFileContent);
787+
}
773788

774-
return this.transpileYamlFile(renderedFile, errorsReport, options);
789+
return this.transpileYamlFile({ ...file, content: renderedFileContent }, errorsReport, options);
775790
}
776791

777792
public withQuery(query, fn) {

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)