-
Notifications
You must be signed in to change notification settings - Fork 680
Reduce string copies and allocations when emitting inlined sourcemaps #1550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package sourcemap | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"slices" | ||
"strings" | ||
"unsafe" | ||
|
||
"github.com/go-json-experiment/json" | ||
"github.com/microsoft/typescript-go/internal/tspath" | ||
) | ||
|
||
|
@@ -333,13 +335,26 @@ | |
} | ||
} | ||
|
||
// Gets the string representation of the source map | ||
func (gen *Generator) String() string { | ||
func (gen *Generator) bytes() []byte { | ||
buf, err := json.Marshal(gen.RawSourceMap()) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
return string(buf) | ||
return buf | ||
} | ||
|
||
// Gets the string representation of the source map | ||
func (gen *Generator) String() string { | ||
return string(gen.bytes()) | ||
} | ||
|
||
func (gen *Generator) Base64DataURL() string { | ||
const prefix = "data:application/json;base64," | ||
data := gen.bytes() | ||
buf := make([]byte, len(prefix)+base64.StdEncoding.EncodedLen(len(data))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can also be done via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, avoiding unsafe is preferred. |
||
copy(buf, prefix) | ||
base64.StdEncoding.Encode(buf[len(prefix):], data) | ||
return unsafe.String(unsafe.SliceData(buf), len(buf)) | ||
} | ||
|
||
func base64FormatEncode(value int) rune { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really sure why you swapped out the json import?