Skip to content

Using strings.Builder in golang v1.10 to reduce memory allocations #367

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.8.x
- 1.10.x
- 1.x

before_install:
Expand Down
7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io"
"reflect"
"strings"
"sync"
"unsafe"

Expand Down Expand Up @@ -79,6 +80,7 @@ type frozenConfig struct {
extraExtensions []Extension
streamPool *sync.Pool
iteratorPool *sync.Pool
stringBuilderPool *sync.Pool
caseSensitive bool
}

Expand Down Expand Up @@ -145,6 +147,11 @@ func (cfg Config) Froze() API {
return NewIterator(api)
},
}
api.stringBuilderPool = &sync.Pool{
New: func() interface{} {
return &strings.Builder{}
},
}
api.initCache()
encoderExtension := EncoderExtension{}
decoderExtension := DecoderExtension{}
Expand Down
5 changes: 5 additions & 0 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jsoniter
import (
"encoding/json"
"fmt"
"github.com/junchih/stringx"
"io"
)

Expand Down Expand Up @@ -72,6 +73,7 @@ type Iterator struct {
cfg *frozenConfig
reader io.Reader
buf []byte
strf *stringx.Factory
head int
tail int
captureStartedAt int
Expand All @@ -86,6 +88,7 @@ func NewIterator(cfg API) *Iterator {
cfg: cfg.(*frozenConfig),
reader: nil,
buf: nil,
strf: stringx.NewFactory(),
head: 0,
tail: 0,
}
Expand All @@ -97,6 +100,7 @@ func Parse(cfg API, reader io.Reader, bufSize int) *Iterator {
cfg: cfg.(*frozenConfig),
reader: reader,
buf: make([]byte, bufSize),
strf: stringx.NewFactory(),
head: 0,
tail: 0,
}
Expand All @@ -108,6 +112,7 @@ func ParseBytes(cfg API, input []byte) *Iterator {
cfg: cfg.(*frozenConfig),
reader: nil,
buf: input,
strf: stringx.NewFactory(),
head: 0,
tail: len(input),
}
Expand Down
93 changes: 48 additions & 45 deletions iter_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsoniter

import (
"fmt"
"strings"
"unicode/utf16"
)

Expand All @@ -12,7 +13,7 @@ func (iter *Iterator) ReadString() (ret string) {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
if c == '"' {
ret = string(iter.buf[iter.head:i])
ret = iter.strf.NewString(iter.buf[iter.head:i])
iter.head = i + 1
return ret
} else if c == '\\' {
Expand All @@ -28,87 +29,93 @@ func (iter *Iterator) ReadString() (ret string) {
iter.skipThreeBytes('u', 'l', 'l')
return ""
}
iter.ReportError("ReadString", `expects " or n, but found `+string([]byte{c}))
iter.ReportError("ReadString", `expects " or n, but found `+iter.strf.NewString([]byte{c}))
return
}

func (iter *Iterator) readStringSlowPath() (ret string) {
var str []byte
func (iter *Iterator) readStringSlowPath() string {
strb := iter.cfg.borrowStringBuilder()
defer iter.cfg.returnStringBuilder(strb)
var c byte
for iter.Error == nil {
c = iter.readByte()
if c == '"' {
return string(str)
return strb.String()
}
if c == '\\' {
c = iter.readByte()
str = iter.readEscapedChar(c, str)
iter.readEscapedChar(c, strb)
} else {
str = append(str, c)
strb.WriteByte(c)
}
}
iter.ReportError("readStringSlowPath", "unexpected end of input")
return
return ""
}

func (iter *Iterator) readEscapedChar(c byte, str []byte) []byte {
func (iter *Iterator) readEscapedChar(c byte, strb *strings.Builder) {
switch c {
case 'u':
r := iter.readU4()
if utf16.IsSurrogate(r) {
c = iter.readByte()
if iter.Error != nil {
return nil
strb.Reset()
return
}
if c != '\\' {
iter.unreadByte()
str = appendRune(str, r)
return str
appendRune(strb, r)
return
}
c = iter.readByte()
if iter.Error != nil {
return nil
strb.Reset()
return
}
if c != 'u' {
str = appendRune(str, r)
return iter.readEscapedChar(c, str)
appendRune(strb, r)
iter.readEscapedChar(c, strb)
return
}
r2 := iter.readU4()
if iter.Error != nil {
return nil
strb.Reset()
return
}
combined := utf16.DecodeRune(r, r2)
if combined == '\uFFFD' {
str = appendRune(str, r)
str = appendRune(str, r2)
appendRune(strb, r)
appendRune(strb, r2)
} else {
str = appendRune(str, combined)
appendRune(strb, combined)
}
} else {
str = appendRune(str, r)
appendRune(strb, r)
}
case '"':
str = append(str, '"')
strb.WriteByte('"')
case '\\':
str = append(str, '\\')
strb.WriteByte('\\')
case '/':
str = append(str, '/')
strb.WriteByte('/')
case 'b':
str = append(str, '\b')
strb.WriteByte('\b')
case 'f':
str = append(str, '\f')
strb.WriteByte('\f')
case 'n':
str = append(str, '\n')
strb.WriteByte('\n')
case 'r':
str = append(str, '\r')
strb.WriteByte('\r')
case 't':
str = append(str, '\t')
strb.WriteByte('\t')
default:
iter.ReportError("readEscapedChar",
`invalid escape char after \`)
return nil
strb.Reset()
return
}
return str
return
}

// ReadStringAsSlice read string from iterator without copying into string form.
Expand Down Expand Up @@ -187,29 +194,25 @@ const (
runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
)

func appendRune(p []byte, r rune) []byte {
func appendRune(p *strings.Builder, r rune) {
// Negative values are erroneous. Making it unsigned addresses the problem.
switch i := uint32(r); {
case i <= rune1Max:
p = append(p, byte(r))
return p
p.WriteByte(byte(r))
case i <= rune2Max:
p = append(p, t2|byte(r>>6))
p = append(p, tx|byte(r)&maskx)
return p
p.WriteByte(t2 | byte(r>>6))
p.WriteByte(tx | byte(r)&maskx)
case i > maxRune, surrogateMin <= i && i <= surrogateMax:
r = runeError
fallthrough
case i <= rune3Max:
p = append(p, t3|byte(r>>12))
p = append(p, tx|byte(r>>6)&maskx)
p = append(p, tx|byte(r)&maskx)
return p
p.WriteByte(t3 | byte(r>>12))
p.WriteByte(tx | byte(r>>6)&maskx)
p.WriteByte(tx | byte(r)&maskx)
default:
p = append(p, t4|byte(r>>18))
p = append(p, tx|byte(r>>12)&maskx)
p = append(p, tx|byte(r>>6)&maskx)
p = append(p, tx|byte(r)&maskx)
return p
p.WriteByte(t4 | byte(r>>18))
p.WriteByte(tx | byte(r>>12)&maskx)
p.WriteByte(tx | byte(r>>6)&maskx)
p.WriteByte(tx | byte(r)&maskx)
}
}
14 changes: 14 additions & 0 deletions misc_tests/jsoniter_nested_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ func Benchmark_jsoniter_nested(b *testing.B) {
}
}

func Benchmark_jsoniter_string(b *testing.B) {
dummy := func(str string) {}
for n := 0; n < b.N; n++ {
func() {
iter := jsoniter.ConfigDefault.BorrowIterator(
[]byte(`"hello world!!! hello world!!! hello world!!!"`),
)
defer jsoniter.ConfigDefault.ReturnIterator(iter)
hello := iter.ReadString()
dummy(hello)
}()
}
}

func readLevel1Hello(iter *jsoniter.Iterator) []Level2 {
l2Array := make([]Level2, 0, 2)
for iter.ReadArray() {
Expand Down
10 changes: 10 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jsoniter

import (
"io"
"strings"
)

// IteratorPool a thread safe pool of iterators with same configuration
Expand Down Expand Up @@ -40,3 +41,12 @@ func (cfg *frozenConfig) ReturnIterator(iter *Iterator) {
iter.Attachment = nil
cfg.iteratorPool.Put(iter)
}

func (cfg *frozenConfig) borrowStringBuilder() *strings.Builder {
return cfg.stringBuilderPool.Get().(*strings.Builder)
}

func (cfg *frozenConfig) returnStringBuilder(builder *strings.Builder) {
builder.Reset()
cfg.stringBuilderPool.Put(builder)
}