commit 50b756fa7ba83632de3569d8236a03887eb25a4f
parent 7fd98627fec0688476bdf9793fb945509a03ab4f
Author: francoispqt <francois@parquet.ninja>
Date: Sun, 29 Apr 2018 00:17:38 +0800
reduce alloc by calling borrowDecoder instead or BorrowDecoder from unmarshal api
Diffstat:
3 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/README.md b/README.md
@@ -1,7 +1,6 @@
[![Build Status](https://travis-ci.org/francoispqt/gojay.svg?branch=master)](https://travis-ci.org/francoispqt/gojay)
[![codecov](https://codecov.io/gh/francoispqt/gojay/branch/master/graph/badge.svg)](https://codecov.io/gh/francoispqt/gojay)
[![Go Report Card](https://goreportcard.com/badge/github.com/francoispqt/gojay)](https://goreportcard.com/report/github.com/francoispqt/gojay)
-[![Sourcegraph](https://sourcegraph.com/github.com/francoispqt/gojay/-/badge.svg)](https://sourcegraph.com/github.com/francoispqt/gojay?badge)
[![Go doc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square
)](https://godoc.org/github.com/francoispqt/gojay)
![MIT License](https://img.shields.io/badge/license-mit-blue.svg?style=flat-square)
diff --git a/decode.go b/decode.go
@@ -13,7 +13,7 @@ import (
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, UnmarshalArray skips that field and completes the unmarshaling as best it can.
func UnmarshalArray(data []byte, v UnmarshalerArray) error {
- dec := BorrowDecoder(nil)
+ dec := borrowDecoder(nil, 0)
defer dec.Release()
dec.data = make([]byte, len(data))
copy(dec.data, data)
@@ -35,7 +35,7 @@ func UnmarshalArray(data []byte, v UnmarshalerArray) error {
// If a JSON value is not appropriate for a given target type, or if a JSON number
// overflows the target type, UnmarshalObject skips that field and completes the unmarshaling as best it can.
func UnmarshalObject(data []byte, v UnmarshalerObject) error {
- dec := BorrowDecoder(nil)
+ dec := borrowDecoder(nil, 0)
defer dec.Release()
dec.data = make([]byte, len(data))
copy(dec.data, data)
@@ -73,53 +73,53 @@ func Unmarshal(data []byte, v interface{}) error {
var dec *Decoder
switch vt := v.(type) {
case *string:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeString(vt)
case *int:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt(vt)
case *int32:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt32(vt)
case *uint32:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint32(vt)
case *int64:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeInt64(vt)
case *uint64:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeUint64(vt)
case *float64:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeFloat64(vt)
case *bool:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = data
err = dec.decodeBool(vt)
case UnmarshalerObject:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = make([]byte, len(data))
copy(dec.data, data)
_, err = dec.decodeObject(vt)
case UnmarshalerArray:
- dec = BorrowDecoder(nil)
+ dec = borrowDecoder(nil, 0)
dec.length = len(data)
dec.data = make([]byte, len(data))
copy(dec.data, data)
diff --git a/encode_interface_test.go b/encode_interface_test.go
@@ -1,6 +1,8 @@
package gojay
import (
+ "fmt"
+ "reflect"
"testing"
"github.com/stretchr/testify/assert"
@@ -113,6 +115,7 @@ var encoderTestCases = []struct {
expectations: func(t *testing.T, b []byte, err error) {
assert.NotNil(t, err, "err should be nil")
assert.IsType(t, InvalidMarshalError(""), err, "err should be of type InvalidMarshalError")
+ assert.Equal(t, fmt.Sprintf(invalidMarshalErrorMsg, reflect.TypeOf(&struct{}{}).String()), err.Error(), "err message should be equal to invalidMarshalErrorMsg")
},
},
}