@@ -71,6 +71,7 @@ export type DataSchemaCompilerOptions = {
71
71
allowNodeRequire ?: boolean ;
72
72
compiledScriptCache : LRUCache < string , vm . Script > ;
73
73
compiledYamlCache : LRUCache < string , string > ;
74
+ compiledJinjaCache : LRUCache < string , string > ;
74
75
} ;
75
76
76
77
export type TranspileOptions = {
@@ -148,6 +149,8 @@ export class DataSchemaCompiler {
148
149
149
150
private readonly compiledYamlCache : LRUCache < string , string > ;
150
151
152
+ private readonly compiledJinjaCache : LRUCache < string , string > ;
153
+
151
154
private compileV8ContextCache : vm . Context | null = null ;
152
155
153
156
// FIXME: Is public only because of tests, should be private
@@ -182,6 +185,7 @@ export class DataSchemaCompiler {
182
185
this . compilerId = options . compilerId || 'default' ;
183
186
this . compiledScriptCache = options . compiledScriptCache ;
184
187
this . compiledYamlCache = options . compiledYamlCache ;
188
+ this . compiledJinjaCache = options . compiledJinjaCache ;
185
189
}
186
190
187
191
public compileObjects ( compileServices : CompilerInterface [ ] , objects , errorsReport : ErrorReporter ) {
@@ -710,17 +714,28 @@ export class DataSchemaCompiler {
710
714
errorsReport : ErrorReporter ,
711
715
options : TranspileOptions
712
716
) : 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
+ }
718
733
719
734
// We update the jinja/yaml file content to the rendered content
720
735
// to reuse the same transpileYamlFile() flow afterward which
721
736
// will update the content to the transpiled js content
722
737
// avoiding costly YAML/Python parsing again.
723
- file . content = renderedFile . content ;
738
+ file . content = renderedFileContent ;
724
739
725
740
return this . transpileYamlFile ( file , errorsReport , options ) ;
726
741
}
0 commit comments