Skip to content

Commit aab67a6

Browse files
committed
introduce compiledJinjaCache
1 parent 28fabf8 commit aab67a6

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) {
@@ -770,13 +774,24 @@ export class DataSchemaCompiler {
770774
errorsReport: ErrorReporter,
771775
options: TranspileOptions
772776
): Promise<(FileContent | undefined)> {
773-
const renderedFile = await this.yamlCompiler.renderTemplate(
774-
file,
775-
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
776-
this.pythonContext!
777-
);
777+
const cacheKey = crypto.createHash('md5').update(JSON.stringify(file.content)).digest('hex');
778+
779+
let renderedFileContent: string;
780+
781+
if (this.compiledJinjaCache.has(cacheKey)) {
782+
renderedFileContent = this.compiledJinjaCache.get(cacheKey)!;
783+
} else {
784+
const renderedFile = await this.yamlCompiler.renderTemplate(
785+
file,
786+
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
787+
this.pythonContext!
788+
);
789+
renderedFileContent = renderedFile.content;
790+
791+
this.compiledJinjaCache.set(cacheKey, renderedFileContent);
792+
}
778793

779-
return this.transpileYamlFile(renderedFile, errorsReport, options);
794+
return this.transpileYamlFile({ ...file, content: renderedFileContent }, errorsReport, options);
780795
}
781796

782797
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)