Skip to content

Commit 7e6dc66

Browse files
committed
introduce compiledYamlCache
1 parent a86c1a5 commit 7e6dc66

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export type DataSchemaCompilerOptions = {
7070
compileContext?: any;
7171
allowNodeRequire?: boolean;
7272
compiledScriptCache: LRUCache<string, vm.Script>;
73+
compiledYamlCache: LRUCache<string, string>;
7374
};
7475

7576
export type TranspileOptions = {
@@ -145,6 +146,8 @@ export class DataSchemaCompiler {
145146

146147
private readonly compiledScriptCache: LRUCache<string, vm.Script>;
147148

149+
private readonly compiledYamlCache: LRUCache<string, string>;
150+
148151
private compileV8ContextCache: vm.Context | null = null;
149152

150153
// FIXME: Is public only because of tests, should be private
@@ -178,6 +181,7 @@ export class DataSchemaCompiler {
178181
this.workerPool = null;
179182
this.compilerId = options.compilerId || 'default';
180183
this.compiledScriptCache = options.compiledScriptCache;
184+
this.compiledYamlCache = options.compiledYamlCache;
181185
}
182186

183187
public compileObjects(compileServices: CompilerInterface[], objects, errorsReport: ErrorReporter) {
@@ -635,6 +639,15 @@ export class DataSchemaCompiler {
635639
errorsReport: ErrorReporter,
636640
{ cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions
637641
): Promise<(FileContent | undefined)> {
642+
const cacheKey = crypto.createHash('md5').update(JSON.stringify(file.content)).digest('hex');
643+
644+
if (this.compiledYamlCache.has(cacheKey)) {
645+
file.content = this.compiledYamlCache.get(cacheKey)!;
646+
file.convertedToJs = true;
647+
648+
return { ...file };
649+
}
650+
638651
/* if (getEnv('transpilationNative')) {
639652
640653
} else */ if (getEnv('transpilationWorkerThreads')) {
@@ -653,6 +666,8 @@ export class DataSchemaCompiler {
653666
file.content = res.content;
654667
file.convertedToJs = true;
655668

669+
this.compiledYamlCache.set(cacheKey, res.content);
670+
656671
return { ...file, content: res.content };
657672
} else {
658673
const transpiledFile = this.yamlCompiler.transpileYamlFile(file, errorsReport);
@@ -665,6 +680,8 @@ export class DataSchemaCompiler {
665680
file.convertedToJs = true;
666681
}
667682

683+
this.compiledYamlCache.set(cacheKey, transpiledFile?.content || '');
684+
668685
return transpiledFile;
669686
}
670687
}

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

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

4243
export interface CompilerInterface {
@@ -59,6 +60,7 @@ export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareComp
5960
const yamlCompiler = new YamlCompiler(cubeSymbols, cubeDictionary, nativeInstance, viewCompiler);
6061

6162
const compiledScriptCache = options.compiledScriptCache || new LRUCache<string, vm.Script>({ max: 250 });
63+
const compiledYamlCache = options.compiledYamlCache || new LRUCache<string, string>({ max: 250 });
6264

6365
const transpilers: TranspilerInterface[] = [
6466
new ValidationTranspiler(),
@@ -79,6 +81,7 @@ export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareComp
7981
transpilers,
8082
viewCompilationGate,
8183
compiledScriptCache,
84+
compiledYamlCache,
8285
viewCompilers: [viewCompiler],
8386
cubeCompilers: [cubeEvaluator, joinGraph, metaTransformer],
8487
contextCompilers: [contextEvaluator],

0 commit comments

Comments
 (0)