mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-17 12:56:09 +00:00
perf(packages/codegen): reduce megamorphic type reads in printDirectivesAndStatements (#27592)
Perf optimization. Continuation of [#27458](https://github.com/oxc-project/oxc/issues/27458). The expression of a leading expression statement can be any expression, so reading its `type` is a megamorphic load (a builtin call, which V8 does not deduplicate). Read its `type` once while unwrapping parens, instead of in `withoutParens` and again after it.
This commit is contained in:
1 parent
256259ddfa
commit
b29f35eac3
1 file changed
+23
-11
@@ -45,7 +45,7 @@ import {
|
||||
} from "./typescript.ts";
|
||||
|
||||
import type { State } from "../state.ts";
|
||||
import type { UnknownNode } from "./types.ts";
|
||||
import type { Literal, UnknownNode } from "./types.ts";
|
||||
import type * as ESTree from "../../../../npm/oxc-types/types.d.ts";
|
||||
|
||||
/**
|
||||
@@ -86,17 +86,29 @@ export function printDirectivesAndStatements(
|
||||
stmt = body[i];
|
||||
} else {
|
||||
// Ensure a string literal (only possible via parentheses, since a bare one would be a directive)
|
||||
// as 1st statement or after other real directives, is not re-parsed as a directive
|
||||
const inner = withoutParens(stmt.expression);
|
||||
if (inner.type === "Literal" && typeof inner.value === "string") {
|
||||
const mapsIndent = state.indentLevel > 0 || state.pendingIndentAsSpace;
|
||||
printIndent(state);
|
||||
if (mapsIndent) markMapStart(state, stmt.start, stmt.end, stmt);
|
||||
writeNoLast(state, "(");
|
||||
printString(state, inner.value, inner.start, inner.end, inner);
|
||||
write(state, ");\n", CAT_OTHER);
|
||||
i++;
|
||||
// as 1st statement or after other real directives, is not re-parsed as a directive.
|
||||
// The expression can be anything, so its `type` is a megamorphic load - read it once while unwrapping parens.
|
||||
let inner = stmt.expression,
|
||||
innerType = inner.type;
|
||||
while (innerType === "ParenthesizedExpression") {
|
||||
inner = (inner as ESTree.ParenthesizedExpression).expression;
|
||||
innerType = inner.type;
|
||||
}
|
||||
|
||||
if (innerType === "Literal") {
|
||||
typeAssertIs<Literal>(inner);
|
||||
const innerValue = inner.value;
|
||||
if (typeof innerValue === "string") {
|
||||
const mapsIndent = state.indentLevel > 0 || state.pendingIndentAsSpace;
|
||||
printIndent(state);
|
||||
if (mapsIndent) markMapStart(state, stmt.start, stmt.end, stmt);
|
||||
writeNoLast(state, "(");
|
||||
printString(state, innerValue, inner.start, inner.end, inner);
|
||||
write(state, ");\n", CAT_OTHER);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user