From 5712f51c574df3c346046a020639addd866e62e1 Mon Sep 17 00:00:00 2001 From: James Yean Date: Wed, 18 Oct 2023 14:56:41 +0800 Subject: [PATCH] refactor: clone bytes (#640) Signed-off-by: James Yean --- lib/bytes/clone_go119.go | 17 +++++++++++++++++ lib/bytes/clone_go120.go | 11 +++++++++++ pkg/template/json/generator.go | 9 ++++++--- pkg/template/text/parse.go | 2 +- server/store/meta/utils.go | 5 ++++- 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 lib/bytes/clone_go119.go create mode 100644 lib/bytes/clone_go120.go diff --git a/lib/bytes/clone_go119.go b/lib/bytes/clone_go119.go new file mode 100644 index 000000000..c76d779ae --- /dev/null +++ b/lib/bytes/clone_go119.go @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: 2023 Linkall Inc. +// +// SPDX-License-Identifier: Apache-2.0 + +//go:build !go1.20 + +package bytes + +// Clone returns a copy of b[:len(b)]. +// The result may have additional unused capacity. +// Clone(nil) returns nil. +func Clone(b []byte) []byte { + if b == nil { + return nil + } + return append([]byte{}, b...) +} diff --git a/lib/bytes/clone_go120.go b/lib/bytes/clone_go120.go new file mode 100644 index 000000000..ccdea72f8 --- /dev/null +++ b/lib/bytes/clone_go120.go @@ -0,0 +1,11 @@ +// SPDX-FileCopyrightText: 2023 Linkall Inc. +// +// SPDX-License-Identifier: Apache-2.0 + +//go:build go1.20 + +package bytes + +import "bytes" + +var Clone = bytes.Clone diff --git a/pkg/template/json/generator.go b/pkg/template/json/generator.go index 94d950ac7..ee5428ed8 100644 --- a/pkg/template/json/generator.go +++ b/pkg/template/json/generator.go @@ -16,15 +16,18 @@ package json import ( // standard libraries. - "bytes" + stdbytes "bytes" // third-party libraries. "github.com/ohler55/ojg/jp" + + // first-party libraries. + "github.com/vanus-labs/vanus/lib/bytes" ) type templateGenerator struct { stack generatorStack - buf bytes.Buffer + buf stdbytes.Buffer segments []templateSegment } @@ -164,7 +167,7 @@ func (g *templateGenerator) packLiteral() { bs := g.buf.Bytes() if len(bs) != 0 { g.segments = append(g.segments, &literalSegment{ - val: append([]byte{}, bs...), + val: bytes.Clone(bs), }) g.buf.Reset() } diff --git a/pkg/template/text/parse.go b/pkg/template/text/parse.go index 8181cc995..193462415 100644 --- a/pkg/template/text/parse.go +++ b/pkg/template/text/parse.go @@ -56,7 +56,7 @@ func parse(text string) ([]templateSegment, error) { return } segments = append(segments, &textSegment{ - text: append([]byte{}, buf.Bytes()...), + text: bytes.Clone(buf.Bytes()), }) buf.Reset() } diff --git a/server/store/meta/utils.go b/server/store/meta/utils.go index d4aab0083..f275d5312 100644 --- a/server/store/meta/utils.go +++ b/server/store/meta/utils.go @@ -17,6 +17,9 @@ package meta import ( // third-party libraries. "github.com/huandu/skiplist" + + // first-party libraries. + "github.com/vanus-labs/vanus/lib/bytes" ) func update(m *skiplist.SkipList, key []byte, value interface{}) { @@ -32,7 +35,7 @@ func set(m *skiplist.SkipList, key []byte, value interface{}) { switch val := value.(type) { case []byte: // Make a copy to avoid modifying value outside. - bs := append([]byte{}, val...) + bs := bytes.Clone(val) m.Set(key, bs) default: m.Set(key, value)