Skip to content

Commit 20c9895

Browse files
committed
perf: avoid cloning "TopLevelSymbolToParts" map
1 parent c76a3da commit 20c9895

File tree

12 files changed

+203
-169
lines changed

12 files changed

+203
-169
lines changed

internal/bundler/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (c *linkerContext) generateExtraDataForFileJS(sourceIndex uint32) string {
4343
sb.WriteString(fmt.Sprintf(`{"isLive":%v`, part.IsLive))
4444
sb.WriteString(fmt.Sprintf(`,"canBeRemovedIfUnused":%v`, part.CanBeRemovedIfUnused))
4545

46-
if partIndex == int(repr.Meta.NSExportPartIndex) {
46+
if partIndex == int(js_ast.NSExportPartIndex) {
4747
sb.WriteString(`,"nsExportPartIndex":true`)
4848
} else if ast.MakeIndex32(uint32(partIndex)) == repr.Meta.WrapperPartIndex {
4949
sb.WriteString(`,"wrapperPartIndex":true`)

internal/bundler/linker.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,19 +1211,13 @@ func (c *linkerContext) scanImportsAndExports() {
12111211
c.addExportsForExportStar(repr.Meta.ResolvedExports, sourceIndex, exportStarStack)
12121212
}
12131213

1214-
// Add an empty part for the namespace export that we can fill in later
1215-
repr.Meta.NSExportPartIndex = c.graph.AddPartToFile(sourceIndex, js_ast.Part{
1216-
CanBeRemovedIfUnused: true,
1217-
})
1218-
12191214
// Also add a special export so import stars can bind to it. This must be
12201215
// done in this step because it must come after CommonJS module discovery
12211216
// but before matching imports with exports.
12221217
repr.Meta.ResolvedExportStar = &graph.ExportData{
12231218
Ref: repr.AST.ExportsRef,
12241219
SourceIndex: sourceIndex,
12251220
}
1226-
repr.AST.TopLevelSymbolToParts[repr.AST.ExportsRef] = []uint32{repr.Meta.NSExportPartIndex}
12271221
}
12281222
c.timer.End("Step 3")
12291223

@@ -1365,17 +1359,17 @@ func (c *linkerContext) scanImportsAndExports() {
13651359
runtimeRepr := c.graph.Files[runtime.SourceIndex].InputFile.Repr.(*graph.JSRepr)
13661360
if repr.Meta.NeedsExportSymbolFromRuntime {
13671361
exportRef := runtimeRepr.AST.ModuleScope.Members["__export"].Ref
1368-
c.graph.GenerateSymbolImportAndUse(sourceIndex, repr.Meta.NSExportPartIndex, exportRef, 1, runtime.SourceIndex)
1362+
c.graph.GenerateSymbolImportAndUse(sourceIndex, js_ast.NSExportPartIndex, exportRef, 1, runtime.SourceIndex)
13691363
}
13701364
if repr.Meta.NeedsMarkAsModuleSymbolFromRuntime {
13711365
markAsModuleRef := runtimeRepr.AST.ModuleScope.Members["__markAsModule"].Ref
1372-
c.graph.GenerateSymbolImportAndUse(sourceIndex, repr.Meta.NSExportPartIndex, markAsModuleRef, 1, runtime.SourceIndex)
1366+
c.graph.GenerateSymbolImportAndUse(sourceIndex, js_ast.NSExportPartIndex, markAsModuleRef, 1, runtime.SourceIndex)
13731367
}
13741368
}
13751369

13761370
for importRef, importData := range repr.Meta.ImportsToBind {
13771371
resolvedRepr := c.graph.Files[importData.SourceIndex].InputFile.Repr.(*graph.JSRepr)
1378-
partsDeclaringSymbol := resolvedRepr.AST.TopLevelSymbolToParts[importData.Ref]
1372+
partsDeclaringSymbol := resolvedRepr.TopLevelSymbolToParts(importData.Ref)
13791373

13801374
for _, partIndex := range repr.AST.NamedImports[importRef].LocalPartsWithUses {
13811375
part := &repr.AST.Parts[partIndex]
@@ -1416,7 +1410,7 @@ func (c *linkerContext) scanImportsAndExports() {
14161410
}
14171411

14181412
// Pull in all declarations of this symbol
1419-
for _, partIndex := range targetRepr.AST.TopLevelSymbolToParts[targetRef] {
1413+
for _, partIndex := range targetRepr.TopLevelSymbolToParts(targetRef) {
14201414
dependencies = append(dependencies, js_ast.Dependency{
14211415
SourceIndex: targetSourceIndex,
14221416
PartIndex: partIndex,
@@ -1428,7 +1422,7 @@ func (c *linkerContext) scanImportsAndExports() {
14281422
if repr.Meta.ForceIncludeExportsForEntryPoint {
14291423
dependencies = append(dependencies, js_ast.Dependency{
14301424
SourceIndex: sourceIndex,
1431-
PartIndex: repr.Meta.NSExportPartIndex,
1425+
PartIndex: js_ast.NSExportPartIndex,
14321426
})
14331427
}
14341428

@@ -1564,7 +1558,7 @@ func (c *linkerContext) generateCodeForLazyExport(sourceIndex uint32) {
15641558
if len(repr.AST.Parts) < 1 {
15651559
panic("Internal error")
15661560
}
1567-
part := &repr.AST.Parts[0]
1561+
part := &repr.AST.Parts[1]
15681562
if len(part.Stmts) != 1 {
15691563
panic("Internal error")
15701564
}
@@ -1706,7 +1700,7 @@ func (c *linkerContext) createExportsForFile(sourceIndex uint32) {
17061700
nsExportSymbolUses[export.Ref] = js_ast.SymbolUse{CountEstimate: 1}
17071701

17081702
// Make sure the part that declares the export is included
1709-
for _, partIndex := range c.graph.Files[export.SourceIndex].InputFile.Repr.(*graph.JSRepr).AST.TopLevelSymbolToParts[export.Ref] {
1703+
for _, partIndex := range c.graph.Files[export.SourceIndex].InputFile.Repr.(*graph.JSRepr).TopLevelSymbolToParts(export.Ref) {
17101704
// Use a non-local dependency since this is likely from a different
17111705
// file if it came in through an export star
17121706
nsExportDependencies = append(nsExportDependencies, js_ast.Dependency{
@@ -1744,7 +1738,7 @@ func (c *linkerContext) createExportsForFile(sourceIndex uint32) {
17441738
}}}})
17451739

17461740
// Make sure this file depends on the "__markAsModule" symbol
1747-
for _, partIndex := range runtimeRepr.AST.TopLevelSymbolToParts[markAsModuleRef] {
1741+
for _, partIndex := range runtimeRepr.TopLevelSymbolToParts(markAsModuleRef) {
17481742
nsExportDependencies = append(nsExportDependencies, js_ast.Dependency{
17491743
SourceIndex: runtime.SourceIndex,
17501744
PartIndex: partIndex,
@@ -1773,7 +1767,7 @@ func (c *linkerContext) createExportsForFile(sourceIndex uint32) {
17731767
}}}})
17741768

17751769
// Make sure this file depends on the "__export" symbol
1776-
for _, partIndex := range runtimeRepr.AST.TopLevelSymbolToParts[exportRef] {
1770+
for _, partIndex := range runtimeRepr.TopLevelSymbolToParts(exportRef) {
17771771
nsExportDependencies = append(nsExportDependencies, js_ast.Dependency{
17781772
SourceIndex: runtime.SourceIndex,
17791773
PartIndex: partIndex,
@@ -1788,7 +1782,7 @@ func (c *linkerContext) createExportsForFile(sourceIndex uint32) {
17881782
if len(nsExportStmts) > 0 {
17891783
// Initialize the part that was allocated for us earlier. The information
17901784
// here will be used after this during tree shaking.
1791-
repr.AST.Parts[repr.Meta.NSExportPartIndex] = js_ast.Part{
1785+
repr.AST.Parts[js_ast.NSExportPartIndex] = js_ast.Part{
17921786
Stmts: nsExportStmts,
17931787
SymbolUses: nsExportSymbolUses,
17941788
Dependencies: nsExportDependencies,
@@ -1829,7 +1823,7 @@ func (c *linkerContext) createWrapperForFile(sourceIndex uint32) {
18291823
case graph.WrapCJS:
18301824
runtimeRepr := c.graph.Files[runtime.SourceIndex].InputFile.Repr.(*graph.JSRepr)
18311825
commonJSRef := runtimeRepr.AST.NamedExports["__commonJS"].Ref
1832-
commonJSParts := runtimeRepr.AST.TopLevelSymbolToParts[commonJSRef]
1826+
commonJSParts := runtimeRepr.TopLevelSymbolToParts(commonJSRef)
18331827

18341828
// Generate the dummy part
18351829
dependencies := make([]js_ast.Dependency, len(commonJSParts))
@@ -1866,7 +1860,7 @@ func (c *linkerContext) createWrapperForFile(sourceIndex uint32) {
18661860
case graph.WrapESM:
18671861
runtimeRepr := c.graph.Files[runtime.SourceIndex].InputFile.Repr.(*graph.JSRepr)
18681862
esmRef := runtimeRepr.AST.NamedExports["__esm"].Ref
1869-
esmParts := runtimeRepr.AST.TopLevelSymbolToParts[esmRef]
1863+
esmParts := runtimeRepr.TopLevelSymbolToParts(esmRef)
18701864

18711865
// Generate the dummy part
18721866
dependencies := make([]js_ast.Dependency, len(esmParts))
@@ -2168,7 +2162,7 @@ loop:
21682162

21692163
// Depend on the statement(s) that declared this import symbol in the
21702164
// original file
2171-
for _, resolvedPartIndex := range c.graph.Files[tracker.sourceIndex].InputFile.Repr.(*graph.JSRepr).AST.TopLevelSymbolToParts[tracker.importRef] {
2165+
for _, resolvedPartIndex := range c.graph.Files[tracker.sourceIndex].InputFile.Repr.(*graph.JSRepr).TopLevelSymbolToParts(tracker.importRef) {
21722166
reExports = append(reExports, js_ast.Dependency{
21732167
SourceIndex: tracker.sourceIndex,
21742168
PartIndex: resolvedPartIndex,
@@ -2933,8 +2927,8 @@ func (c *linkerContext) chunkFileOrder(chunk *chunkInfo) (js []uint32, jsParts [
29332927

29342928
// Make sure the generated call to "__export(exports, ...)" comes first
29352929
// before anything else in this file
2936-
if canFileBeSplit && isFileInThisChunk && repr.AST.Parts[repr.Meta.NSExportPartIndex].IsLive {
2937-
jsParts = appendOrExtendPartRange(jsParts, sourceIndex, repr.Meta.NSExportPartIndex)
2930+
if canFileBeSplit && isFileInThisChunk && repr.AST.Parts[js_ast.NSExportPartIndex].IsLive {
2931+
jsParts = appendOrExtendPartRange(jsParts, sourceIndex, js_ast.NSExportPartIndex)
29382932
}
29392933

29402934
for partIndex, part := range repr.AST.Parts {
@@ -2955,7 +2949,7 @@ func (c *linkerContext) chunkFileOrder(chunk *chunkInfo) (js []uint32, jsParts [
29552949
// Then include this part after the files it imports
29562950
if isPartInThisChunk {
29572951
isFileInThisChunk = true
2958-
if canFileBeSplit && uint32(partIndex) != repr.Meta.NSExportPartIndex && c.shouldIncludePart(repr, part) {
2952+
if canFileBeSplit && uint32(partIndex) != js_ast.NSExportPartIndex && c.shouldIncludePart(repr, part) {
29592953
if sourceIndex == runtime.SourceIndex {
29602954
jsPartsPrefix = appendOrExtendPartRange(jsPartsPrefix, sourceIndex, uint32(partIndex))
29612955
} else {
@@ -3388,7 +3382,7 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
33883382
) {
33893383
file := &c.graph.Files[partRange.sourceIndex]
33903384
repr := file.InputFile.Repr.(*graph.JSRepr)
3391-
nsExportPartIndex := repr.Meta.NSExportPartIndex
3385+
nsExportPartIndex := js_ast.NSExportPartIndex
33923386
needsWrapper := false
33933387
stmtList := stmtList{}
33943388

internal/bundler/snapshots/snapshots_css.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ TestCSSAndJavaScriptCodeSplittingIssue1064
1818
---------- /out/a.js ----------
1919
import {
2020
shared_default
21-
} from "./chunk-JHSNRTG6.js";
21+
} from "./chunk-XTGNVFM6.js";
2222

2323
// a.js
2424
console.log(shared_default() + 1);
2525

2626
---------- /out/b.js ----------
2727
import {
2828
shared_default
29-
} from "./chunk-JHSNRTG6.js";
29+
} from "./chunk-XTGNVFM6.js";
3030

3131
// b.js
3232
console.log(shared_default() + 2);
3333

34-
---------- /out/chunk-JHSNRTG6.js ----------
34+
---------- /out/chunk-XTGNVFM6.js ----------
3535
// shared.js
3636
function shared_default() {
3737
return 3;

internal/bundler/snapshots/snapshots_default.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,20 +203,20 @@ new c();
203203
TestCharFreqIgnoreComments
204204
---------- /out/a.js ----------
205205
// a.js
206-
function o(e, t, n, r) {
206+
function u(e, t, n, r) {
207207
return "the argument names must be the same";
208208
}
209209
export {
210-
o as default
210+
u as default
211211
};
212212

213213
---------- /out/b.js ----------
214214
// b.js
215-
function o(e, t, n, r) {
215+
function u(e, t, n, r) {
216216
return "the argument names must be the same";
217217
}
218218
export {
219-
o as default
219+
u as default
220220
};
221221

222222
================================================================================
@@ -1770,7 +1770,7 @@ console.log(shared_default);
17701770
================================================================================
17711771
TestMinifiedBundleCommonJS
17721772
---------- /out.js ----------
1773-
var t=r(n=>{n.foo=function(){return 123}});var s=r((l,u)=>{u.exports={test:!0}});var{foo:c}=t();console.log(c(),s());
1773+
var t=r(n=>{n.foo=function(){return 123}});var u=r((l,s)=>{s.exports={test:!0}});var{foo:c}=t();console.log(c(),u());
17741774

17751775
================================================================================
17761776
TestMinifiedBundleES6
@@ -3080,26 +3080,26 @@ var import_es6_ns_export_abstract_class = __toModule(require_es6_ns_export_abstr
30803080
TestTopLevelAwaitAllowedImportWithSplitting
30813081
---------- /out/entry.js ----------
30823082
// entry.js
3083-
import("./a-6SYMAPP6.js");
3084-
import("./b-A7BBMSAV.js");
3085-
import("./c-745DPZZ6.js");
3083+
import("./a-3BAWOBN3.js");
3084+
import("./b-2IGVSUS7.js");
3085+
import("./c-DMBKURS2.js");
30863086
require_entry();
30873087
await 0;
30883088

3089-
---------- /out/a-6SYMAPP6.js ----------
3089+
---------- /out/a-3BAWOBN3.js ----------
30903090
import "./chunk-QJYGFXJG.js";
3091-
import "./chunk-G3F22LPE.js";
3091+
import "./chunk-GETF6B5C.js";
30923092

3093-
---------- /out/b-A7BBMSAV.js ----------
3093+
---------- /out/b-2IGVSUS7.js ----------
30943094
import "./chunk-QJYGFXJG.js";
3095-
import "./chunk-G3F22LPE.js";
3095+
import "./chunk-GETF6B5C.js";
30963096

30973097
---------- /out/chunk-QJYGFXJG.js ----------
30983098

3099-
---------- /out/c-745DPZZ6.js ----------
3100-
import "./chunk-G3F22LPE.js";
3099+
---------- /out/c-DMBKURS2.js ----------
3100+
import "./chunk-GETF6B5C.js";
31013101

3102-
---------- /out/chunk-G3F22LPE.js ----------
3102+
---------- /out/chunk-GETF6B5C.js ----------
31033103
// c.js
31043104
await 0;
31053105

0 commit comments

Comments
 (0)