Skip to content

Commit

Permalink
Merge pull request #188 from zonghaishang/perf_decode_string
Browse files Browse the repository at this point in the history
Perf decode string
  • Loading branch information
zonghaishang committed May 28, 2020
2 parents dd0ef4b + af95c64 commit 323b16b
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 36 deletions.
41 changes: 41 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,54 @@ func NewDecoder(b []byte) *Decoder {
return &Decoder{reader: bufio.NewReader(bytes.NewReader(b)), typeRefs: &TypeRefs{records: map[string]bool{}}}
}

// NewDecoder generate a decoder instance
func NewDecoderSize(b []byte, size int) *Decoder {
return &Decoder{reader: bufio.NewReaderSize(bytes.NewReader(b), size), typeRefs: &TypeRefs{records: map[string]bool{}}}
}

// NewDecoder generate a decoder instance with skip
func NewDecoderWithSkip(b []byte) *Decoder {
return &Decoder{reader: bufio.NewReader(bytes.NewReader(b)), typeRefs: &TypeRefs{records: map[string]bool{}}, isSkip: true}
}

// NewCheapDecoderWithSkip generate a decoder instance with skip,
// only for cache pool, before decode Reset should be called.
// For example, with pooling use, will effectively improve performance
//
// var hessianPool = &sync.Pool{
// New: func() interface{} {
// return hessian.NewCheapDecoderWithSkip([]byte{})
// },
// }
//
// decoder := hessianPool.Get().(*hessian.Decoder)
// fill decode data
// decoder.Reset(data[:])
// decode anything ...
// hessianPool.Put(decoder)
func NewCheapDecoderWithSkip(b []byte) *Decoder {
return &Decoder{reader: bufio.NewReader(bytes.NewReader(b)), isSkip: true}
}

/////////////////////////////////////////
// utilities
/////////////////////////////////////////

func (d *Decoder) Reset(b []byte) *Decoder {
// reuse reader buf, avoid allocate
d.reader.Reset(bytes.NewReader(b))
d.typeRefs = &TypeRefs{records: map[string]bool{}}

if d.refs != nil {
d.refs = nil
}
if d.classInfoList != nil {
d.classInfoList = nil
}

return d
}

// peek a byte
func (d *Decoder) peekByte() byte {
return d.peek(1)[0]
Expand Down Expand Up @@ -150,6 +189,8 @@ func (d *Decoder) Decode() (interface{}, error) {
return EnsureInterface(d.DecodeValue())
}

func (d *Decoder) Buffered() int { return d.reader.Buffered() }

// DecodeValue parse hessian data, the return value maybe a reflection value when it's a map, list, object, or ref.
func (d *Decoder) DecodeValue() (interface{}, error) {
var (
Expand Down
1 change: 1 addition & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

const (
hessianJar = "test_hessian/target/test_hessian-1.0.0.jar"
testString = "hello, world! 你好,世界!"
)

func isFileExist(file string) bool {
Expand Down
Loading

0 comments on commit 323b16b

Please sign in to comment.