Skip to content

Commit 25ef517

Browse files
committed
attempt to optimize py parser calls
1 parent 38212b9 commit 25ef517

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,19 @@ export class YamlCompiler {
149149
const fullPath = propertyPath.join('.');
150150
if (fullPath.match(p)) {
151151
if (typeof obj === 'string' && ['sql', 'sqlTable'].includes(propertyPath[propertyPath.length - 1])) {
152-
return this.parsePythonIntoArrowFunction(`f"${this.escapeDoubleQuotes(obj)}"`, cubeName, obj, errorsReport);
152+
if (obj.match(PY_TEMPLATE_SYNTAX)) {
153+
return this.parsePythonIntoArrowFunction(`f"${this.escapeDoubleQuotes(obj)}"`, cubeName, obj, errorsReport);
154+
} else {
155+
// Optimization: directly create arrow function returning string instead of parsing Python
156+
return t.arrowFunctionExpression([], t.stringLiteral(obj));
157+
}
153158
} else if (typeof obj === 'string') {
154-
return this.parsePythonIntoArrowFunction(obj, cubeName, obj, errorsReport);
159+
if (obj.match(PY_TEMPLATE_SYNTAX)) {
160+
return this.parsePythonIntoArrowFunction(obj, cubeName, obj, errorsReport);
161+
} else {
162+
// Optimization: directly create arrow function returning identifier instead of parsing Python
163+
return this.astIntoArrowFunction(t.program([t.expressionStatement(t.identifier(obj))]), obj, cubeName);
164+
}
155165
} else if (Array.isArray(obj)) {
156166
const resultAst = t.program([t.expressionStatement(t.arrayExpression(obj.map(code => {
157167
let ast: t.Program | t.NullLiteral | t.BooleanLiteral | t.NumericLiteral | t.StringLiteral | null = null;
@@ -172,7 +182,7 @@ export class YamlCompiler {
172182
} else if (code instanceof Date) {
173183
// Special case when dates are defined in YAML as strings without quotes
174184
// YAML parser treats them as Date objects, but for conversion we need them as strings
175-
ast = this.parsePythonAndTranspileToJs(`f"${this.escapeDoubleQuotes(code.toISOString())}"`, errorsReport);
185+
ast = t.stringLiteral(code.toISOString());
176186
}
177187
}
178188
if (ast === null) {
@@ -196,14 +206,26 @@ export class YamlCompiler {
196206
} else if (typeof obj === 'string') {
197207
let code = obj;
198208

209+
if (obj === '') {
210+
return t.nullLiteral();
211+
}
212+
213+
if (code.match(PY_TEMPLATE_SYNTAX)) {
214+
if (!nonStringFields.has(propertyPath[propertyPath.length - 1])) {
215+
code = `f"${this.escapeDoubleQuotes(obj)}"`;
216+
}
217+
218+
const parsePythonAndTranspileToJsTimer225 = perfTracker.start('parsePythonAndTranspileToJs call 225');
219+
const ast = this.parsePythonAndTranspileToJs(code, errorsReport);
220+
parsePythonAndTranspileToJsTimer225.end();
221+
return this.extractProgramBodyIfNeeded(ast);
222+
}
223+
199224
if (!nonStringFields.has(propertyPath[propertyPath.length - 1])) {
200-
code = `f"${this.escapeDoubleQuotes(obj)}"`;
225+
return t.templateLiteral([t.templateElement({ raw: code, cooked: code })], []);
201226
}
202227

203-
const parsePythonAndTranspileToJsTimer225 = perfTracker.start('parsePythonAndTranspileToJs call 225');
204-
const ast = this.parsePythonAndTranspileToJs(code, errorsReport);
205-
parsePythonAndTranspileToJsTimer225.end();
206-
return this.extractProgramBodyIfNeeded(ast);
228+
return t.identifier(code);
207229
} else if (typeof obj === 'boolean') {
208230
return t.booleanLiteral(obj);
209231
} else if (typeof obj === 'number') {

0 commit comments

Comments
 (0)