Skip to content

Commit ac8d673

Browse files
refactor: use maps.Copy to simplify the code (#4741)
Signed-off-by: withtimezone <with_timezone@outlook.com> Co-authored-by: CoderZhi <thecoderzhi@gmail.com>
1 parent 3957c79 commit ac8d673

File tree

5 files changed

+14
-24
lines changed

5 files changed

+14
-24
lines changed

action/protocol/execution/evm/access_list.go

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

88
import (
9+
"maps"
10+
911
"github.com/ethereum/go-ethereum/common"
1012
)
1113

@@ -46,9 +48,7 @@ func newAccessList() *accessList {
4648
// Copy creates an independent copy of an accessList.
4749
func (al *accessList) Copy() *accessList {
4850
cp := newAccessList()
49-
for k, v := range al.addresses {
50-
cp.addresses[k] = v
51-
}
51+
maps.Copy(cp.addresses, al.addresses)
5252
cp.slots = make([]map[common.Hash]struct{}, len(al.slots))
5353
for i, slotMap := range al.slots {
5454
newSlotmap := make(map[common.Hash]struct{}, len(slotMap))

action/protocol/execution/evm/evmstatedbadapter.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"encoding/hex"
1212
"fmt"
13+
"maps"
1314
"math/big"
1415
"sort"
1516

@@ -800,9 +801,7 @@ func (stateDB *StateDBAdapter) Snapshot() int {
800801
}
801802
// save a copy of current SelfDestruct accounts
802803
sa := make(deleteAccount)
803-
for k, v := range stateDB.selfDestructed {
804-
sa[k] = v
805-
}
804+
maps.Copy(sa, stateDB.selfDestructed)
806805
stateDB.selfDestructedSnapshot[sn] = sa
807806
if !stateDB.fixSnapshotOrder {
808807
for _, addr := range stateDB.cachedContractAddrs() {
@@ -812,9 +811,7 @@ func (stateDB *StateDBAdapter) Snapshot() int {
812811
stateDB.contractSnapshot[sn] = c
813812
// save a copy of preimages
814813
p := make(preimageMap)
815-
for k, v := range stateDB.preimages {
816-
p[k] = v
817-
}
814+
maps.Copy(p, stateDB.preimages)
818815
stateDB.preimageSnapshot[sn] = p
819816
// save a copy of access list
820817
stateDB.accessListSnapshot[sn] = stateDB.accessList.Copy()
@@ -823,9 +820,7 @@ func (stateDB *StateDBAdapter) Snapshot() int {
823820
stateDB.transientStorageSnapshot[sn] = stateDB.transientStorage.Copy()
824821
// save a copy of created account map
825822
ca := make(createdAccount)
826-
for k, v := range stateDB.createdAccount {
827-
ca[k] = v
828-
}
823+
maps.Copy(ca, stateDB.createdAccount)
829824
stateDB.createdAccountSnapshot[sn] = ca
830825
}
831826
return sn

blockindex/contractstaking/cache.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package contractstaking
88
import (
99
"context"
1010
"log"
11+
"maps"
1112
"math/big"
1213
"sync"
1314

@@ -289,9 +290,7 @@ func (s *contractStakingCache) Clone() stakingCache {
289290
c.candidateBucketMap = make(map[string]map[uint64]bool, len(s.candidateBucketMap))
290291
for k, v := range s.candidateBucketMap {
291292
c.candidateBucketMap[k] = make(map[uint64]bool, len(v))
292-
for k1, v1 := range v {
293-
c.candidateBucketMap[k][k1] = v1
294-
}
293+
maps.Copy(c.candidateBucketMap[k], v)
295294
}
296295
c.bucketTypeMap = make(map[uint64]*BucketType, len(s.bucketTypeMap))
297296
for k, v := range s.bucketTypeMap {
@@ -300,9 +299,7 @@ func (s *contractStakingCache) Clone() stakingCache {
300299
c.propertyBucketTypeMap = make(map[int64]map[uint64]uint64, len(s.propertyBucketTypeMap))
301300
for k, v := range s.propertyBucketTypeMap {
302301
c.propertyBucketTypeMap[k] = make(map[uint64]uint64, len(v))
303-
for k1, v1 := range v {
304-
c.propertyBucketTypeMap[k][k1] = v1
305-
}
302+
maps.Copy(c.propertyBucketTypeMap[k], v)
306303
}
307304
c.deltaBucketTypes = make(map[uint64]*BucketType, len(s.deltaBucketTypes))
308305
for k, v := range s.deltaBucketTypes {

blockindex/contractstaking/wrappedcache.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package contractstaking
77

88
import (
99
"context"
10+
"maps"
1011
"math/big"
1112
"sort"
1213
"sync"
@@ -271,9 +272,7 @@ func (wc *wrappedCache) Clone() stakingCache {
271272
updatedCandidates := make(map[string]map[uint64]bool, len(wc.updatedCandidates))
272273
for delegate, buckets := range wc.updatedCandidates {
273274
updatedBuckets := make(map[uint64]bool, len(buckets))
274-
for id, updated := range buckets {
275-
updatedBuckets[id] = updated
276-
}
275+
maps.Copy(updatedBuckets, buckets)
277276
updatedCandidates[delegate] = updatedBuckets
278277
}
279278
return &wrappedCache{

dispatcher/dispatcher.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package dispatcher
77

88
import (
99
"context"
10+
"maps"
1011
"sync"
1112
"time"
1213

@@ -186,9 +187,7 @@ func (d *IotxDispatcher) EventAudit() map[iotexrpc.MessageType]int {
186187
d.eventAuditLock.RLock()
187188
defer d.eventAuditLock.RUnlock()
188189
snapshot := make(map[iotexrpc.MessageType]int)
189-
for k, v := range d.eventAudit {
190-
snapshot[k] = v
191-
}
190+
maps.Copy(snapshot, d.eventAudit)
192191
return snapshot
193192
}
194193

0 commit comments

Comments
 (0)