From 64c7c09590b922a5a8689bbf6dc0152c11e83d40 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Wed, 17 Mar 2021 22:09:16 -0700 Subject: [PATCH] move the unrelated "Joiner" out of "js_printer" --- internal/bundler/linker.go | 7 ++-- internal/helpers/joiner.go | 57 +++++++++++++++++++++++++++++ internal/js_printer/js_printer.go | 61 ++----------------------------- 3 files changed, 64 insertions(+), 61 deletions(-) create mode 100644 internal/helpers/joiner.go diff --git a/internal/bundler/linker.go b/internal/bundler/linker.go index db8ff42dcd7..c19a7ef5a70 100644 --- a/internal/bundler/linker.go +++ b/internal/bundler/linker.go @@ -17,6 +17,7 @@ import ( "github.com/evanw/esbuild/internal/css_ast" "github.com/evanw/esbuild/internal/css_printer" "github.com/evanw/esbuild/internal/fs" + "github.com/evanw/esbuild/internal/helpers" "github.com/evanw/esbuild/internal/js_ast" "github.com/evanw/esbuild/internal/js_lexer" "github.com/evanw/esbuild/internal/js_printer" @@ -3789,7 +3790,7 @@ func (repr *chunkReprJS) generate(c *linkerContext, chunk *chunkInfo) func(gener waitGroup.Wait() - j := js_printer.Joiner{} + j := helpers.Joiner{} prevOffset := sourcemap.LineColumnOffset{} // Optionally strip whitespace @@ -4263,7 +4264,7 @@ func (repr *chunkReprCSS) generate(c *linkerContext, chunk *chunkInfo) func(gene // Wait for cross-chunk import records before continuing return func(continueData generateContinue) []OutputFile { waitGroup.Wait() - j := js_printer.Joiner{} + j := helpers.Joiner{} newlineBeforeComment := false if len(c.options.CSSBanner) > 0 { @@ -4513,7 +4514,7 @@ func (c *linkerContext) generateSourceMapForChunk( chunkAbsDir string, dataForSourceMaps []dataForSourceMap, ) []byte { - j := js_printer.Joiner{} + j := helpers.Joiner{} j.AddString("{\n \"version\": 3") // Only write out the sources for a given source index once diff --git a/internal/helpers/joiner.go b/internal/helpers/joiner.go new file mode 100644 index 00000000000..4dadae0079b --- /dev/null +++ b/internal/helpers/joiner.go @@ -0,0 +1,57 @@ +package helpers + +// This provides an efficient way to join lots of big string and byte slices +// together. It avoids the cost of repeatedly reallocating as the buffer grows +// by measuring exactly how big the buffer should be and then allocating once. +// This is a measurable speedup. +type Joiner struct { + lastByte byte + strings []joinerString + bytes []joinerBytes + length uint32 +} + +type joinerString struct { + data string + offset uint32 +} + +type joinerBytes struct { + data []byte + offset uint32 +} + +func (j *Joiner) AddString(data string) { + if len(data) > 0 { + j.lastByte = data[len(data)-1] + } + j.strings = append(j.strings, joinerString{data, j.length}) + j.length += uint32(len(data)) +} + +func (j *Joiner) AddBytes(data []byte) { + if len(data) > 0 { + j.lastByte = data[len(data)-1] + } + j.bytes = append(j.bytes, joinerBytes{data, j.length}) + j.length += uint32(len(data)) +} + +func (j *Joiner) LastByte() byte { + return j.lastByte +} + +func (j *Joiner) Length() uint32 { + return j.length +} + +func (j *Joiner) Done() []byte { + buffer := make([]byte, j.length) + for _, item := range j.strings { + copy(buffer[item.offset:], item.data) + } + for _, item := range j.bytes { + copy(buffer[item.offset:], item.data) + } + return buffer +} diff --git a/internal/js_printer/js_printer.go b/internal/js_printer/js_printer.go index 3d8dbcdb29d..ab580d3ebfd 100644 --- a/internal/js_printer/js_printer.go +++ b/internal/js_printer/js_printer.go @@ -11,6 +11,7 @@ import ( "github.com/evanw/esbuild/internal/ast" "github.com/evanw/esbuild/internal/compat" "github.com/evanw/esbuild/internal/config" + "github.com/evanw/esbuild/internal/helpers" "github.com/evanw/esbuild/internal/js_ast" "github.com/evanw/esbuild/internal/js_lexer" "github.com/evanw/esbuild/internal/logger" @@ -45,7 +46,7 @@ type SourceMapState struct { // After all chunks are computed, they are joined together in a second pass. // This rewrites the first mapping in each chunk to be relative to the end // state of the previous chunk. -func AppendSourceMapChunk(j *Joiner, prevEndState SourceMapState, startState SourceMapState, sourceMap []byte) { +func AppendSourceMapChunk(j *helpers.Joiner, prevEndState SourceMapState, startState SourceMapState, sourceMap []byte) { // Handle line breaks in between this mapping and the previous one if startState.GeneratedLine != 0 { j.AddBytes(bytes.Repeat([]byte{';'}, startState.GeneratedLine)) @@ -80,7 +81,7 @@ func AppendSourceMapChunk(j *Joiner, prevEndState SourceMapState, startState Sou startState.GeneratedColumn += generatedColumn startState.OriginalLine += originalLine startState.OriginalColumn += originalColumn - j.AddBytes(appendMapping(nil, j.lastByte, prevEndState, startState)) + j.AddBytes(appendMapping(nil, j.LastByte(), prevEndState, startState)) // Then append everything after that without modification. j.AddBytes(sourceMap) @@ -111,62 +112,6 @@ func appendMapping(buffer []byte, lastByte byte, prevState SourceMapState, curre return buffer } -// This provides an efficient way to join lots of big string and byte slices -// together. It avoids the cost of repeatedly reallocating as the buffer grows -// by measuring exactly how big the buffer should be and then allocating once. -// This is a measurable speedup. -type Joiner struct { - lastByte byte - strings []joinerString - bytes []joinerBytes - length uint32 -} - -type joinerString struct { - data string - offset uint32 -} - -type joinerBytes struct { - data []byte - offset uint32 -} - -func (j *Joiner) AddString(data string) { - if len(data) > 0 { - j.lastByte = data[len(data)-1] - } - j.strings = append(j.strings, joinerString{data, j.length}) - j.length += uint32(len(data)) -} - -func (j *Joiner) AddBytes(data []byte) { - if len(data) > 0 { - j.lastByte = data[len(data)-1] - } - j.bytes = append(j.bytes, joinerBytes{data, j.length}) - j.length += uint32(len(data)) -} - -func (j *Joiner) LastByte() byte { - return j.lastByte -} - -func (j *Joiner) Length() uint32 { - return j.length -} - -func (j *Joiner) Done() []byte { - buffer := make([]byte, j.length) - for _, item := range j.strings { - copy(buffer[item.offset:], item.data) - } - for _, item := range j.bytes { - copy(buffer[item.offset:], item.data) - } - return buffer -} - const hexChars = "0123456789ABCDEF" const firstASCII = 0x20 const lastASCII = 0x7E