perf(minify): skip renaming symbols with zero use count

In MinifyRenamer, avoid generating names for slots where the symbol
use count is zero. These represent declared symbols that are never
actually used (e.g., tree-shaken away or declared but not referenced).

We filter out zero-count slots before adding to the sorted array,
avoiding unnecessary sorting and iteration overhead. This is
particularly beneficial when nested scopes are tree-shaken, as their
pre-allocated symbol slots would otherwise still be processed.

Ported from oxc: https://github.com/oxc-project/oxc/pull/18183

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Claude Bot
2026-01-19 05:04:22 +00:00
parent 716801e92d
commit ab9ba5be4e
2 changed files with 37 additions and 4 deletions

View File

@@ -272,13 +272,14 @@ pub const MinifyRenamer = struct {
var slots = this.slots.getPtr(ns);
sorted.clearRetainingCapacity();
try sorted.ensureUnusedCapacity(slots.items.len);
sorted.items.len = slots.items.len;
for (sorted.items, slots.items, 0..) |*elem, slot, i| {
elem.* = SlotAndCount{
for (slots.items, 0..) |slot, i| {
// Skip symbols with zero use count - they're never used and don't need a minified name
if (slot.count == 0) continue;
sorted.appendAssumeCapacity(SlotAndCount{
.slot = @as(u32, @intCast(i)),
.count = slot.count,
};
});
}
std.sort.pdq(SlotAndCount, sorted.items, {}, SlotAndCount.lessThan);

View File

@@ -1135,4 +1135,36 @@ describe("bundler", () => {
);
},
});
// Regression test for: https://github.com/oxc-project/oxc/pull/18183
// When symbols have zero use count, we skip them during renaming.
// This tests that when nested scopes are tree-shaken, their pre-allocated
// symbol slots (which have count=0) are skipped during name assignment.
itBundled("minify/SkipUnusedSymbolSlots", {
files: {
"/entry.js": /* js */ `
// Create nested scopes with many symbols that will be tree-shaken
function unused() {
var a = 1, b = 2, c = 3;
function inner() {
var x = 1, y = 2, z = 3, w = 4, v = 5;
return x + y + z + w + v;
}
return a + b + c + inner();
}
// This function is used and should get short minified names
function used() { return 42; }
console.log(used());
`,
},
minifyIdentifiers: true,
minifyWhitespace: true,
minifySyntax: true,
onAfterBundle(api) {
const code = api.readFile("/out.js").trim();
// The unused function and its nested scopes are tree-shaken.
// The optimization ensures we don't waste time generating names for
// the pre-allocated slots that were never used.
expect(code).toMatchInlineSnapshot(`"function j(){return 42}console.log(j());"`);
},
});
});