Skip to content

Commit 4a07b17

Browse files
committed
reorder struct fields to reduce memory/add speed
This runs Go's "fieldalignment" pass and fixes all reports.
1 parent 08c9aa9 commit 4a07b17

39 files changed

+619
-593
lines changed

cmd/esbuild/service.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ type serveStopCallback func()
3131
type pluginResolveCallback func(uint32, map[string]interface{}) []byte
3232

3333
type activeBuild struct {
34-
mutex sync.Mutex
35-
refCount int
3634
rebuild rebuildCallback
3735
watchStop watchStopCallback
3836
serveStop serveStopCallback
3937
pluginResolve pluginResolveCallback
38+
mutex sync.Mutex
39+
refCount int
4040
}
4141

4242
type serviceType struct {
43-
mutex sync.Mutex
4443
callbacks map[uint32]responseCallback
4544
activeBuilds map[int]*activeBuild
46-
nextRequestID uint32
4745
outgoingPackets chan outgoingPacket
4846
keepAliveWaitGroup sync.WaitGroup
47+
mutex sync.Mutex
48+
nextRequestID uint32
4949
}
5050

5151
func (service *serviceType) getActiveBuild(key int) *activeBuild {

cmd/esbuild/stdio_protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func readLengthPrefixedSlice(bytes []byte) (slice []byte, leftOver []byte, ok bo
3434
}
3535

3636
type packet struct {
37+
value interface{}
3738
id uint32
3839
isRequest bool
39-
value interface{}
4040
}
4141

4242
func encodePacket(p packet) []byte {

internal/ast/ast.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ func (flags ImportRecordFlags) Has(flag ImportRecordFlags) bool {
117117
}
118118

119119
type ImportRecord struct {
120-
Range logger.Range
121-
Path logger.Path
122120
Assertions *[]AssertEntry
121+
Path logger.Path
122+
Range logger.Range
123123

124124
// The resolved source index for an internal import (within the bundle) or
125125
// nil for an external import (not included in the bundle)
126126
SourceIndex Index32
127127

128-
Kind ImportKind
129128
Flags ImportRecordFlags
129+
Kind ImportKind
130130
}
131131

132132
type AssertEntry struct {

internal/bundler/bundler.go

+23-20
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ import (
3535
)
3636

3737
type scannerFile struct {
38-
inputFile graph.InputFile
39-
pluginData interface{}
40-
4138
// If "AbsMetadataFile" is present, this will be filled out with information
4239
// about this file in JSON format. This is a partial JSON file that will be
4340
// fully assembled later.
4441
jsonMetadataChunk string
42+
43+
pluginData interface{}
44+
inputFile graph.InputFile
4545
}
4646

4747
// This is data related to source maps. It's computed in parallel with linking
@@ -60,40 +60,40 @@ type dataForSourceMap struct {
6060
}
6161

6262
type Bundle struct {
63-
fs fs.FS
64-
res resolver.Resolver
65-
files []scannerFile
66-
entryPoints []graph.EntryPoint
67-
6863
// The unique key prefix is a random string that is unique to every bundling
6964
// operation. It is used as a prefix for the unique keys assigned to every
7065
// chunk during linking. These unique keys are used to identify each chunk
7166
// before the final output paths have been computed.
7267
uniqueKeyPrefix string
68+
69+
fs fs.FS
70+
res resolver.Resolver
71+
files []scannerFile
72+
entryPoints []graph.EntryPoint
7373
}
7474

7575
type parseArgs struct {
7676
fs fs.FS
7777
log logger.Log
7878
res resolver.Resolver
7979
caches *cache.CacheSet
80-
keyPath logger.Path
8180
prettyPath string
82-
sourceIndex uint32
8381
importSource *logger.Source
8482
sideEffects graph.SideEffects
85-
importPathRange logger.Range
8683
pluginData interface{}
87-
options config.Options
8884
results chan parseResult
8985
inject chan config.InjectedFile
90-
skipResolve bool
9186
uniqueKeyPrefix string
87+
keyPath logger.Path
88+
options config.Options
89+
importPathRange logger.Range
90+
sourceIndex uint32
91+
skipResolve bool
9292
}
9393

9494
type parseResult struct {
95-
file scannerFile
9695
resolveResults []*resolver.ResolveResult
96+
file scannerFile
9797
tlaCheck tlaCheck
9898
ok bool
9999
}
@@ -810,10 +810,10 @@ func RunOnResolvePlugins(
810810
}
811811

812812
type loaderPluginResult struct {
813-
loader config.Loader
813+
pluginData interface{}
814814
absResolveDir string
815815
pluginName string
816-
pluginData interface{}
816+
loader config.Loader
817817
}
818818

819819
func runOnLoadPlugins(
@@ -982,17 +982,20 @@ type scanner struct {
982982
fs fs.FS
983983
res resolver.Resolver
984984
caches *cache.CacheSet
985-
options config.Options
986985
timer *helpers.Timer
987986
uniqueKeyPrefix string
988987

989-
// This is not guarded by a mutex because it's only ever modified by a single
988+
// These are not guarded by a mutex because it's only ever modified by a single
990989
// thread. Note that not all results in the "results" array are necessarily
991990
// valid. Make sure to check the "ok" flag before using them.
992991
results []parseResult
993992
visited map[logger.Path]uint32
994993
resultChannel chan parseResult
995-
remaining int
994+
995+
options config.Options
996+
997+
// Also not guarded by a mutex for the same reason
998+
remaining int
996999
}
9971000

9981001
type EntryPoint struct {
@@ -2328,8 +2331,8 @@ type runtimeCacheKey struct {
23282331
}
23292332

23302333
type runtimeCache struct {
2331-
astMutex sync.Mutex
23322334
astMap map[runtimeCacheKey]js_ast.AST
2335+
astMutex sync.Mutex
23332336
}
23342337

23352338
var globalRuntimeCache runtimeCache

internal/bundler/bundler_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ type bundled struct {
5959
}
6060

6161
type suite struct {
62+
expectedSnapshots map[string]string
63+
generatedSnapshots map[string]string
6264
name string
6365
path string
6466
mutex sync.Mutex
65-
expectedSnapshots map[string]string
66-
generatedSnapshots map[string]string
6767
}
6868

6969
func (s *suite) expectBundled(t *testing.T, args bundled) {

internal/bundler/linker.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ type linkerContext struct {
4141
// This helps avoid an infinite loop when matching imports to exports
4242
cycleDetector []importTracker
4343

44-
// We may need to refer to the CommonJS "module" symbol for exports
45-
unboundModuleRef js_ast.Ref
46-
47-
// We may need to refer to the "__esm" and/or "__commonJS" runtime symbols
48-
cjsRuntimeRef js_ast.Ref
49-
esmRuntimeRef js_ast.Ref
50-
5144
// This represents the parallel computation of source map related data.
5245
// Calling this will block until the computation is done. The resulting value
5346
// is shared between threads and must be treated as immutable.
@@ -56,6 +49,13 @@ type linkerContext struct {
5649
// This is passed to us from the bundling phase
5750
uniqueKeyPrefix string
5851
uniqueKeyPrefixBytes []byte // This is just "uniqueKeyPrefix" in byte form
52+
53+
// We may need to refer to the CommonJS "module" symbol for exports
54+
unboundModuleRef js_ast.Ref
55+
56+
// We may need to refer to the "__esm" and/or "__commonJS" runtime symbols
57+
cjsRuntimeRef js_ast.Ref
58+
esmRuntimeRef js_ast.Ref
5959
}
6060

6161
type partRange struct {
@@ -72,11 +72,6 @@ type chunkInfo struct {
7272
filesWithPartsInChunk map[uint32]bool
7373
entryBits helpers.BitSet
7474

75-
// This information is only useful if "isEntryPoint" is true
76-
isEntryPoint bool
77-
sourceIndex uint32 // An index into "c.sources"
78-
entryPointBit uint // An index into "c.graph.EntryPoints"
79-
8075
// For code splitting
8176
crossChunkImports []chunkImport
8277

@@ -94,11 +89,6 @@ type chunkInfo struct {
9489
// If non-empty, this chunk needs to generate an external legal comments file.
9590
externalLegalComments []byte
9691

97-
// When this chunk is initially generated in isolation, the output pieces
98-
// will contain slices of the output with the unique keys of other chunks
99-
// omitted.
100-
intermediateOutput intermediateOutput
101-
10292
// This contains the hash for just this chunk without including information
10393
// from the hashes of other chunks. Later on in the linking process, the
10494
// final hash for this chunk will be constructed by merging the isolated
@@ -109,7 +99,18 @@ type chunkInfo struct {
10999
// Other fields relating to the output file for this chunk
110100
jsonMetadataChunkCallback func(finalOutputSize int) helpers.Joiner
111101
outputSourceMap sourcemap.SourceMapPieces
112-
isExecutable bool
102+
103+
// When this chunk is initially generated in isolation, the output pieces
104+
// will contain slices of the output with the unique keys of other chunks
105+
// omitted.
106+
intermediateOutput intermediateOutput
107+
108+
// This information is only useful if "isEntryPoint" is true
109+
entryPointBit uint // An index into "c.graph.EntryPoints"
110+
sourceIndex uint32 // An index into "c.sources"
111+
isEntryPoint bool
112+
113+
isExecutable bool
113114
}
114115

115116
type chunkImport struct {
@@ -141,15 +142,15 @@ type outputPiece struct {
141142
}
142143

143144
type intermediateOutput struct {
145+
// If the chunk has references to other chunks, then "pieces" contains the
146+
// contents of the chunk and "joiner" should not be used. Another joiner
147+
// will have to be constructed later when merging the pieces together.
148+
pieces []outputPiece
149+
144150
// If the chunk doesn't have any references to other chunks, then "pieces" is
145151
// nil and "joiner" contains the contents of the chunk. This is more efficient
146152
// because it avoids doing a join operation twice.
147153
joiner helpers.Joiner
148-
149-
// Otherwise, "pieces" contains the contents of the chunk and "joiner" should
150-
// not be used. Another joiner will have to be constructed later when merging
151-
// the pieces together.
152-
pieces []outputPiece
153154
}
154155

155156
type chunkRepr interface{ isChunk() }
@@ -162,10 +163,10 @@ type chunkReprJS struct {
162163
partsInChunkInOrder []partRange
163164

164165
// For code splitting
165-
crossChunkPrefixStmts []js_ast.Stmt
166-
crossChunkSuffixStmts []js_ast.Stmt
167166
exportsToOtherChunks map[js_ast.Ref]string
168167
importsFromOtherChunks map[uint32]crossChunkImportItemArray
168+
crossChunkPrefixStmts []js_ast.Stmt
169+
crossChunkSuffixStmts []js_ast.Stmt
169170
}
170171

171172
type chunkReprCSS struct {
@@ -994,8 +995,8 @@ func (c *linkerContext) computeCrossChunkDependencies(chunks []chunkInfo) {
994995
}
995996

996997
type crossChunkImport struct {
997-
chunkIndex uint32
998998
sortedImportItems crossChunkImportItemArray
999+
chunkIndex uint32
9991000
}
10001001

10011002
// This type is just so we can use Go's native sort function
@@ -1031,8 +1032,8 @@ func (c *linkerContext) sortedCrossChunkImports(chunks []chunkInfo, importsFromO
10311032
}
10321033

10331034
type crossChunkImportItem struct {
1034-
ref js_ast.Ref
10351035
exportAlias string
1036+
ref js_ast.Ref
10361037
}
10371038

10381039
// This type is just so we can use Go's native sort function
@@ -2128,9 +2129,9 @@ const (
21282129
)
21292130

21302131
type matchImportResult struct {
2132+
alias string
21312133
kind matchImportKind
21322134
namespaceRef js_ast.Ref
2133-
alias string
21342135
sourceIndex uint32
21352136
nameLoc logger.Loc // Optional, goes with sourceIndex, ignore if zero
21362137
otherSourceIndex uint32
@@ -2859,8 +2860,8 @@ func (c *linkerContext) findImportedCSSFilesInJSOrder(entryPoint uint32) (order
28592860
// traversal order is B D C A.
28602861
func (c *linkerContext) findImportedFilesInCSSOrder(entryPoints []uint32) (externalOrder []externalImportCSS, internalOrder []uint32) {
28612862
type externalImportsCSS struct {
2862-
unconditional bool
28632863
conditions [][]css_ast.Token
2864+
unconditional bool
28642865
}
28652866

28662867
visited := make(map[uint32]bool)
@@ -4893,13 +4894,12 @@ func (c *linkerContext) generateGlobalNamePrefix() string {
48934894
type compileResultCSS struct {
48944895
css_printer.PrintResult
48954896

4896-
sourceIndex uint32
4897-
48984897
// This is the line and column offset since the previous CSS string
48994898
// or the start of the file if this is the first CSS string.
49004899
generatedOffset sourcemap.LineColumnOffset
49014900

4902-
hasCharset bool
4901+
sourceIndex uint32
4902+
hasCharset bool
49034903
}
49044904

49054905
func (c *linkerContext) generateChunkCSS(chunks []chunkInfo, chunkIndex int, chunkWaitGroup *sync.WaitGroup) {

internal/cache/cache.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import (
3232
// reused even if the contents of that "package.json" file have changed.
3333
//
3434
type CacheSet struct {
35-
SourceIndexCache SourceIndexCache
3635
FSCache FSCache
3736
CSSCache CSSCache
3837
JSONCache JSONCache
3938
JSCache JSCache
39+
SourceIndexCache SourceIndexCache
4040
}
4141

4242
func MakeCacheSet() *CacheSet {
@@ -61,8 +61,8 @@ func MakeCacheSet() *CacheSet {
6161
}
6262

6363
type SourceIndexCache struct {
64-
mutex sync.Mutex
6564
entries map[sourceIndexKey]uint32
65+
mutex sync.Mutex
6666
nextSourceIndex uint32
6767
}
6868

0 commit comments

Comments
 (0)