mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Preallocate string before unescaping
This reduces number of allocations in BenchmarkEJ_Unmarshal_M-8 test from 52 down to 46. Signed-off-by: Kirill Korotaev <kirillx@gmail.com>
This commit is contained in:
+8
-1
@@ -260,6 +260,7 @@ func findStringLen(data []byte) (isValid bool, length int) {
|
||||
// if no escaping is needed, original string is returned, otherwise - a new one allocated
|
||||
func (r *Lexer) unescapeStringToken() (err error) {
|
||||
data := r.token.byteValue
|
||||
wasEscaped := false
|
||||
var unescapedData []byte
|
||||
|
||||
for {
|
||||
@@ -273,6 +274,12 @@ func (r *Lexer) unescapeStringToken() (err error) {
|
||||
r.errParse(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
if !wasEscaped {
|
||||
unescapedData = make([]byte, 0, len(r.token.byteValue))
|
||||
wasEscaped = true
|
||||
}
|
||||
|
||||
var d [4]byte
|
||||
s := utf8.EncodeRune(d[:], escapedRune)
|
||||
unescapedData = append(unescapedData, data[:i]...)
|
||||
@@ -281,7 +288,7 @@ func (r *Lexer) unescapeStringToken() (err error) {
|
||||
data = data[i+escapedBytes:]
|
||||
}
|
||||
|
||||
if len(unescapedData) > 0 {
|
||||
if wasEscaped {
|
||||
r.token.byteValue = append(unescapedData, data...)
|
||||
r.token.byteValueCloned = true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user