mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-17 12:56:09 +00:00
perf(packages/codegen): remove all printSpaceBeforeIdentifier calls (#27620)
Perf optimization. #27455 removed the only call to `printSpaceBeforeIdentifier` which isn't a no-op in pretty-printing mode (the only mode that `oxc-codegen` currently supports). Remove `printSpaceBeforeIdentifier` from builds with a `MINIFY` flag which TSDown sets to `false`. Minifier then removes all the calls. Produces a 0.5%-1% perf improvement on benchmarks. Reason for using `MINIFY` flag to render the code a no-op, rather than removing all the calls manually, it's it's useful to have the logic in place, in case we want to support minified output in future. It'd be a pain to restore it all if we do that later.
This commit is contained in:
1 parent
70ecfddf2a
commit
e1c97f8c3f
3 files changed
+32
No files matched your search
Vendored
+5
@@ -13,6 +13,11 @@ declare const TS: boolean;
|
||||
// (see `src/asserts.ts`). Release builds strip them in `tsdown_plugins/remove_asserts.ts`.
|
||||
declare const DEBUG: boolean;
|
||||
|
||||
// `true` if build produces minified output.
|
||||
// Always `false` at present - the printer only produces pretty output. Code which only a minifying printer
|
||||
// would need sits behind it, so it costs pretty builds nothing, but is kept ready for a minify mode.
|
||||
declare const MINIFY: boolean;
|
||||
|
||||
// `true` if is benchmark build.
|
||||
// Built with `pnpm run bench`, which sets `BENCHMARKS=true`. Benchmark builds honor the
|
||||
// `skipSourcemapGeneration` option, so a benchmark can measure the print pass without the final
|
||||
|
||||
@@ -47,6 +47,31 @@ if (DEBUG) {
|
||||
export function printSpaceBeforeIdentifier(state: State): void {
|
||||
debugAssertLastFresh(state);
|
||||
|
||||
// Pretty output never needs this space, so this function is always a no-op.
|
||||
// Only a minifying printer needs the check.
|
||||
//
|
||||
// `last` is never one of the categories the check looks for:
|
||||
//
|
||||
// - The next thing written after a keyword is always either a space (`return x`, `function f`),
|
||||
// or punctuation (`return;`, `default:`, `function(`, `function<`).
|
||||
// A space replaces the keyword's `CAT_IDENT` with `CAT_OTHER` before anything which calls this.
|
||||
// Punctuation does not start with an identifier character, so is never preceded by a call to this.
|
||||
//
|
||||
// - Everything else which writes one of the relevant categories ends a name, an expression or a type:
|
||||
// - an identifier, `this` or `true` (`CAT_IDENT`)
|
||||
// - a number (`CAT_INT_DIGIT`)
|
||||
// - a regex (`CAT_REGEX_SLASH`)
|
||||
// Nothing starting with an identifier character is ever written straight after one of those.
|
||||
// The keywords which can follow them (`in`, `of`, `as`, `from`, `extends` etc)
|
||||
// are always written with a space before them, so do not call this function.
|
||||
//
|
||||
// Release builds remove the calls entirely. Debug builds keep the assertion above,
|
||||
// to ensure `last` stays fresh at every call site, ready for a minify mode.
|
||||
if (!MINIFY) {
|
||||
debugAssert(state.last > CAT_REGEX_SLASH);
|
||||
return;
|
||||
}
|
||||
|
||||
// `last` starts as `CAT_OTHER` (start of output behaves like after whitespace),
|
||||
// so no empty-output check is needed.
|
||||
// Everything needing a space before an identifier is one of the lowest codes, so this is one compare -
|
||||
|
||||
@@ -25,9 +25,11 @@ const assertPlugins = DEBUG ? [] : [removeAssertsPlugin];
|
||||
|
||||
// Global constants defined at build time. See `src-js/globals.d.ts`.
|
||||
// `DEBUG: false` lets the minifier remove the body of `debugAssert` and any other debug-only code.
|
||||
// `MINIFY` is always `false` - the printer only produces pretty output at present.
|
||||
const definedGlobals = {
|
||||
DEBUG: DEBUG ? "true" : "false",
|
||||
BENCHMARKS: BENCHMARKS ? "true" : "false",
|
||||
MINIFY: "false",
|
||||
};
|
||||
|
||||
// Base config.
|
||||
|
||||
Reference in new issue
Block a user