From 6a0cd8e093b032fdcfef26283dddd334d83bb227 Mon Sep 17 00:00:00 2001 From: Calvin Xiao Date: Tue, 6 Apr 2021 10:35:24 +0800 Subject: [PATCH] types/json: replace binary.Read with binary.BigEndian.Uint16 (#23845) --- types/json/binary_functions.go | 7 ++----- types/json/binary_functions_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/types/json/binary_functions.go b/types/json/binary_functions.go index 9f76ffb5d3..3a9e21d1a7 100644 --- a/types/json/binary_functions.go +++ b/types/json/binary_functions.go @@ -137,11 +137,8 @@ func decodeEscapedUnicode(s []byte) (char [4]byte, size int, err error) { // The unicode must can be represented in 2 bytes. return char, 0, errors.Trace(err) } - var unicode uint16 - err = binary.Read(bytes.NewReader(char[0:2]), binary.BigEndian, &unicode) - if err != nil { - return char, 0, errors.Trace(err) - } + + unicode := binary.BigEndian.Uint16(char[0:2]) size = utf8.RuneLen(rune(unicode)) utf8.EncodeRune(char[0:size], rune(unicode)) return diff --git a/types/json/binary_functions_test.go b/types/json/binary_functions_test.go index 1d63e880e8..8191638f7b 100644 --- a/types/json/binary_functions_test.go +++ b/types/json/binary_functions_test.go @@ -13,6 +13,8 @@ package json import ( + "testing" + . "github.com/pingcap/check" ) @@ -29,3 +31,10 @@ func (s *testJSONFuncSuite) TestdecodeEscapedUnicode(c *C) { c.Assert(err, IsNil) } + +func BenchmarkDecodeEscapedUnicode(b *testing.B) { + for i := 0; i < b.N; i++ { + in := "597d" + decodeEscapedUnicode([]byte(in)) + } +}