From 1adac3869548f70ed21716164bd700aaa0e748a9 Mon Sep 17 00:00:00 2001 From: IncSW <dev@incsw.in> Date: Mon, 3 May 2021 13:43:05 +0300 Subject: [PATCH 1/3] remove extra dereference --- decode_slice.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/decode_slice.go b/decode_slice.go index b00a2bb..727caef 100644 --- a/decode_slice.go +++ b/decode_slice.go @@ -127,7 +127,7 @@ func (d *sliceDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) er // assign new element to the slice typedmemmove(d.elemType, ep, unsafe_New(d.elemType)) } else if d.isElemPointerType { - **(**unsafe.Pointer)(unsafe.Pointer(&ep)) = nil // initialize elem pointer + *(*unsafe.Pointer)(ep) = nil // initialize elem pointer } if err := d.valueDecoder.decodeStream(s, depth, ep); err != nil { return err @@ -240,7 +240,7 @@ func (d *sliceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) // assign new element to the slice typedmemmove(d.elemType, ep, unsafe_New(d.elemType)) } else if d.isElemPointerType { - **(**unsafe.Pointer)(unsafe.Pointer(&ep)) = nil // initialize elem pointer + *(*unsafe.Pointer)(ep) = nil // initialize elem pointer } c, err := d.valueDecoder.decode(buf, cursor, depth, ep) if err != nil { From f8aac1e69fbe7aceb71da5ba958c245e7f845c49 Mon Sep 17 00:00:00 2001 From: IncSW <dev@incsw.in> Date: Mon, 3 May 2021 13:44:44 +0300 Subject: [PATCH 2/3] replace skipWhiteSpace goto by loop --- decode_context.go | 4 +--- decode_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/decode_context.go b/decode_context.go index eac8163..38ab7c8 100644 --- a/decode_context.go +++ b/decode_context.go @@ -20,10 +20,8 @@ func char(ptr unsafe.Pointer, offset int64) byte { } func skipWhiteSpace(buf []byte, cursor int64) int64 { -LOOP: - if isWhiteSpace[buf[cursor]] { + for isWhiteSpace[buf[cursor]] { cursor++ - goto LOOP } return cursor } diff --git a/decode_test.go b/decode_test.go index 002a8eb..771878f 100644 --- a/decode_test.go +++ b/decode_test.go @@ -3508,3 +3508,46 @@ func TestDecodeBackSlash(t *testing.T) { }) }) } + +var ( + isWhiteSpace = [256]bool{} +) + +func init() { + isWhiteSpace[' '] = true + isWhiteSpace['\n'] = true + isWhiteSpace['\t'] = true + isWhiteSpace['\r'] = true +} + +func skipWhiteSpace(buf []byte, cursor int64) int64 { +LOOP: + if isWhiteSpace[buf[cursor]] { + cursor++ + goto LOOP + } + return cursor +} + +func skipWhiteSpaceLoop(buf []byte, cursor int64) int64 { + for isWhiteSpace[buf[cursor]] { + cursor++ + } + return cursor +} + +func BenchmarkSkipWhiteSpace(b *testing.B) { + data := []byte(" \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t}") + b.ReportAllocs() + b.ResetTimer() + b.Run("gotoLabel", func(b *testing.B) { + for i := 0; i < b.N; i++ { + skipWhiteSpace(data, 0) + } + }) + b.Run("loop", func(b *testing.B) { + for i := 0; i < b.N; i++ { + skipWhiteSpaceLoop(data, 0) + } + }) +} From a52bba909949ee2d8ab360a8cfd9af92fc132e73 Mon Sep 17 00:00:00 2001 From: IncSW <dev@incsw.in> Date: Wed, 5 May 2021 21:31:22 +0300 Subject: [PATCH 3/3] revert initialize elem pointer & remove unnecessary benchmark --- decode_slice.go | 4 ++-- decode_test.go | 43 ------------------------------------------- 2 files changed, 2 insertions(+), 45 deletions(-) diff --git a/decode_slice.go b/decode_slice.go index 727caef..b00a2bb 100644 --- a/decode_slice.go +++ b/decode_slice.go @@ -127,7 +127,7 @@ func (d *sliceDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) er // assign new element to the slice typedmemmove(d.elemType, ep, unsafe_New(d.elemType)) } else if d.isElemPointerType { - *(*unsafe.Pointer)(ep) = nil // initialize elem pointer + **(**unsafe.Pointer)(unsafe.Pointer(&ep)) = nil // initialize elem pointer } if err := d.valueDecoder.decodeStream(s, depth, ep); err != nil { return err @@ -240,7 +240,7 @@ func (d *sliceDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) // assign new element to the slice typedmemmove(d.elemType, ep, unsafe_New(d.elemType)) } else if d.isElemPointerType { - *(*unsafe.Pointer)(ep) = nil // initialize elem pointer + **(**unsafe.Pointer)(unsafe.Pointer(&ep)) = nil // initialize elem pointer } c, err := d.valueDecoder.decode(buf, cursor, depth, ep) if err != nil { diff --git a/decode_test.go b/decode_test.go index 771878f..002a8eb 100644 --- a/decode_test.go +++ b/decode_test.go @@ -3508,46 +3508,3 @@ func TestDecodeBackSlash(t *testing.T) { }) }) } - -var ( - isWhiteSpace = [256]bool{} -) - -func init() { - isWhiteSpace[' '] = true - isWhiteSpace['\n'] = true - isWhiteSpace['\t'] = true - isWhiteSpace['\r'] = true -} - -func skipWhiteSpace(buf []byte, cursor int64) int64 { -LOOP: - if isWhiteSpace[buf[cursor]] { - cursor++ - goto LOOP - } - return cursor -} - -func skipWhiteSpaceLoop(buf []byte, cursor int64) int64 { - for isWhiteSpace[buf[cursor]] { - cursor++ - } - return cursor -} - -func BenchmarkSkipWhiteSpace(b *testing.B) { - data := []byte(" \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t \n\r\t}") - b.ReportAllocs() - b.ResetTimer() - b.Run("gotoLabel", func(b *testing.B) { - for i := 0; i < b.N; i++ { - skipWhiteSpace(data, 0) - } - }) - b.Run("loop", func(b *testing.B) { - for i := 0; i < b.N; i++ { - skipWhiteSpaceLoop(data, 0) - } - }) -}