Merge branch 'master' of github.com:goccy/go-json into feature/improve-decoder-performance

This commit is contained in:
Masaaki Goshima 2021-02-06 22:15:26 +09:00
commit e9f947a29e
4 changed files with 55 additions and 20 deletions

View File

@ -15,6 +15,8 @@ linters:
- dogsled
- dupl
- exhaustive
- exhaustivestruct
- errorlint
- funlen
- gci
- gochecknoglobals
@ -28,6 +30,16 @@ linters:
- gofumpt
- gomnd
- gosec
- ifshort
- lll
- makezero
- nakedret
- nestif
- nlreturn
- paralleltest
- testpackage
- thelper
- wrapcheck
- interfacer
- lll
- nakedret

View File

@ -1,3 +1,8 @@
BIN_DIR := $(CURDIR)/bin
$(BIN_DIR):
@mkdir -p $(BIN_DIR)
.PHONY: cover
cover:
@ go test -coverprofile=cover.tmp.out . ; \
@ -7,3 +12,17 @@ cover:
.PHONY: cover-html
cover-html: cover
go tool cover -html=cover.out
.PHONY: lint
lint: golangci-lint
golangci-lint run
golangci-lint: | $(BIN_DIR)
@{ \
set -e; \
GOLANGCI_LINT_TMP_DIR=$$(mktemp -d); \
cd $$GOLANGCI_LINT_TMP_DIR; \
go mod init tmp; \
GOBIN=$(BIN_DIR) go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.36.0; \
rm -rf $$GOLANGCI_LINT_TMP_DIR; \
}

View File

@ -23,6 +23,10 @@ type sliceHeader struct {
cap int
}
const (
defaultSliceCapacity = 2
)
func newSliceDecoder(dec decoder, elemType *rtype, size uintptr, structName, fieldName string) *sliceDecoder {
return &sliceDecoder{
valueDecoder: dec,
@ -30,11 +34,10 @@ func newSliceDecoder(dec decoder, elemType *rtype, size uintptr, structName, fie
size: size,
arrayPool: sync.Pool{
New: func() interface{} {
cap := 2
return &sliceHeader{
data: newArray(elemType, cap),
data: newArray(elemType, defaultSliceCapacity),
len: 0,
cap: cap,
cap: defaultSliceCapacity,
}
},
},
@ -84,14 +87,14 @@ func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
}
idx := 0
slice := d.newSlice()
cap := slice.cap
capacity := slice.cap
data := slice.data
for {
if cap <= idx {
src := sliceHeader{data: data, len: idx, cap: cap}
cap *= 2
data = newArray(d.elemType, cap)
dst := sliceHeader{data: data, len: idx, cap: cap}
if capacity <= idx {
src := sliceHeader{data: data, len: idx, cap: capacity}
capacity *= 2
data = newArray(d.elemType, capacity)
dst := sliceHeader{data: data, len: idx, cap: capacity}
copySlice(d.elemType, dst, src)
}
if err := d.valueDecoder.decodeStream(s, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size)); err != nil {
@ -101,7 +104,7 @@ func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
RETRY:
switch s.char() {
case ']':
slice.cap = cap
slice.cap = capacity
slice.len = idx + 1
slice.data = data
dstCap := idx + 1
@ -125,12 +128,12 @@ func (d *sliceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
if s.read() {
goto RETRY
}
slice.cap = cap
slice.cap = capacity
slice.data = data
d.releaseSlice(slice)
goto ERROR
default:
slice.cap = cap
slice.cap = capacity
slice.data = data
d.releaseSlice(slice)
goto ERROR
@ -184,14 +187,14 @@ func (d *sliceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64
}
idx := 0
slice := d.newSlice()
cap := slice.cap
capacity := slice.cap
data := slice.data
for {
if cap <= idx {
src := sliceHeader{data: data, len: idx, cap: cap}
cap *= 2
data = newArray(d.elemType, cap)
dst := sliceHeader{data: data, len: idx, cap: cap}
if capacity <= idx {
src := sliceHeader{data: data, len: idx, cap: capacity}
capacity *= 2
data = newArray(d.elemType, capacity)
dst := sliceHeader{data: data, len: idx, cap: capacity}
copySlice(d.elemType, dst, src)
}
c, err := d.valueDecoder.decode(buf, cursor, unsafe.Pointer(uintptr(data)+uintptr(idx)*d.size))
@ -202,7 +205,7 @@ func (d *sliceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64
cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] {
case ']':
slice.cap = cap
slice.cap = capacity
slice.len = idx + 1
slice.data = data
dstCap := idx + 1
@ -223,7 +226,7 @@ func (d *sliceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64
case ',':
idx++
default:
slice.cap = cap
slice.cap = capacity
slice.data = data
d.releaseSlice(slice)
return 0, errInvalidCharacter(buf[cursor], "slice", cursor)

View File

@ -396,6 +396,7 @@ func expand(b byte) uint64 {
return lsb * uint64(b)
}
//nolint:govet
func stringToUint64Slice(s string) []uint64 {
return *(*[]uint64)(unsafe.Pointer(&reflect.SliceHeader{
Data: ((*reflect.StringHeader)(unsafe.Pointer(&s))).Data,