From 5fd655251b614cd6b936a3301f79d1f8df1a28ab Mon Sep 17 00:00:00 2001 From: Spongecaptain Date: Sat, 5 Mar 2022 12:06:31 +0800 Subject: [PATCH] performance opt: using copy instead of append --- memlog.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/memlog.go b/memlog.go index 15a728b..62f1fd1 100644 --- a/memlog.go +++ b/memlog.go @@ -44,8 +44,8 @@ func (r Record) deepCopy() Record { if r.Metadata.Offset == 0 && r.Metadata.Created.IsZero() { return Record{} } - - dCopy := append([]byte(nil), r.Data...) + dCopy := make([]byte, len(r.Data)) + copy(dCopy, r.Data) return Record{ Metadata: Header{ Offset: r.Metadata.Offset, @@ -139,13 +139,14 @@ func (l *Log) write(ctx context.Context, data []byte) (Offset, error) { return -1, errors.New("no data provided") } - dcopy := append([]byte(nil), data...) + dCopy := make([]byte, len(data)) + copy(dCopy, data) r := Record{ Metadata: Header{ Offset: l.offset, Created: l.clock.Now().UTC(), }, - Data: dcopy, + Data: dCopy, } err := l.active.write(ctx, r)