Use pre-allocated buffer for zero character

This commit is contained in:
Masaaki Goshima 2021-06-03 15:44:48 +09:00
parent fee2d1fc68
commit 80719cf181
2 changed files with 8 additions and 4 deletions

View File

@ -79,6 +79,10 @@ var (
} }
) )
var (
numZeroBuf = []byte{'0'}
)
func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) { func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) {
for { for {
switch s.char() { switch s.char() {
@ -106,7 +110,7 @@ func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) {
return num, nil return num, nil
case '0': case '0':
s.cursor++ s.cursor++
return []byte{'0'}, nil return numZeroBuf, nil
case '1', '2', '3', '4', '5', '6', '7', '8', '9': case '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := s.cursor start := s.cursor
for { for {
@ -150,7 +154,7 @@ func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error)
continue continue
case '0': case '0':
cursor++ cursor++
return []byte{'0'}, cursor, nil return numZeroBuf, cursor, nil
case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9': case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor start := cursor
cursor++ cursor++

View File

@ -62,7 +62,7 @@ func (d *uintDecoder) decodeStreamByte(s *stream) ([]byte, error) {
continue continue
case '0': case '0':
s.cursor++ s.cursor++
return []byte{'0'}, nil return numZeroBuf, nil
case '1', '2', '3', '4', '5', '6', '7', '8', '9': case '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := s.cursor start := s.cursor
for { for {
@ -104,7 +104,7 @@ func (d *uintDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error
continue continue
case '0': case '0':
cursor++ cursor++
return []byte{'0'}, cursor, nil return numZeroBuf, cursor, nil
case '1', '2', '3', '4', '5', '6', '7', '8', '9': case '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := cursor start := cursor
cursor++ cursor++