From 80719cf1819e7b786fc8752234663126d2107d95 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Thu, 3 Jun 2021 15:44:48 +0900 Subject: [PATCH] Use pre-allocated buffer for zero character --- decode_int.go | 8 ++++++-- decode_uint.go | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/decode_int.go b/decode_int.go index 625cc06..d351366 100644 --- a/decode_int.go +++ b/decode_int.go @@ -79,6 +79,10 @@ var ( } ) +var ( + numZeroBuf = []byte{'0'} +) + func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) { for { switch s.char() { @@ -106,7 +110,7 @@ func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) { return num, nil case '0': s.cursor++ - return []byte{'0'}, nil + return numZeroBuf, nil case '1', '2', '3', '4', '5', '6', '7', '8', '9': start := s.cursor for { @@ -150,7 +154,7 @@ func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) continue case '0': cursor++ - return []byte{'0'}, cursor, nil + return numZeroBuf, cursor, nil case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9': start := cursor cursor++ diff --git a/decode_uint.go b/decode_uint.go index 91a0004..f6c19e4 100644 --- a/decode_uint.go +++ b/decode_uint.go @@ -62,7 +62,7 @@ func (d *uintDecoder) decodeStreamByte(s *stream) ([]byte, error) { continue case '0': s.cursor++ - return []byte{'0'}, nil + return numZeroBuf, nil case '1', '2', '3', '4', '5', '6', '7', '8', '9': start := s.cursor for { @@ -104,7 +104,7 @@ func (d *uintDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error continue case '0': cursor++ - return []byte{'0'}, cursor, nil + return numZeroBuf, cursor, nil case '1', '2', '3', '4', '5', '6', '7', '8', '9': start := cursor cursor++