Skip to content

Commit 90d4c2a

Browse files
authored
refactor: use maps.Copy to simplify the code (#4744)
Signed-off-by: rocksload <rocksload@outlook.com>
1 parent ac8d673 commit 90d4c2a

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

db/batch/kv_cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package batch
77

8+
import "maps"
9+
810
type (
911
// KVStoreCache is a local cache of batched <k, v> for fast query
1012
KVStoreCache interface {
@@ -145,9 +147,7 @@ func (c *kvCache) Append(caches ...KVStoreCache) error {
145147
if _, ok := c.cache[key1]; !ok {
146148
c.cache[key1] = make(map[string]*node)
147149
}
148-
for key2, node := range ns {
149-
c.cache[key1][key2] = node
150-
}
150+
maps.Copy(c.cache[key1], ns)
151151
}
152152
}
153153
return nil

db/trie/mptrie/branchnode.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package mptrie
77

88
import (
9+
"maps"
10+
911
"github.com/pkg/errors"
1012
"google.golang.org/protobuf/proto"
1113

@@ -248,9 +250,7 @@ func (b *branchNode) updateChild(cli client, key byte, child node) (node, error)
248250
var indices *SortedList
249251
// update branchnode with new child
250252
children := make(map[byte]node, len(b.children))
251-
for k, v := range b.children {
252-
children[k] = v
253-
}
253+
maps.Copy(children, b.children)
254254
if child == nil {
255255
delete(children, key)
256256
if b.indices.sorted {
@@ -277,9 +277,7 @@ func (b *branchNode) updateChild(cli client, key byte, child node) (node, error)
277277

278278
func (b *branchNode) Clone() (branch, error) {
279279
children := make(map[byte]node, len(b.children))
280-
for key, child := range b.children {
281-
children[key] = child
282-
}
280+
maps.Copy(children, b.children)
283281
hashVal := make([]byte, len(b.hashVal))
284282
copy(hashVal, b.hashVal)
285283
ser := make([]byte, len(b.ser))

0 commit comments

Comments
 (0)