diff --git a/database/batch.go b/database/batch.go index f3187a1fa954..8a37ee723693 100644 --- a/database/batch.go +++ b/database/batch.go @@ -75,6 +75,7 @@ func (b *BatchOps) Size() int { } func (b *BatchOps) Reset() { + clear(b.Ops) b.Ops = b.Ops[:0] b.size = 0 } diff --git a/database/encdb/db.go b/database/encdb/db.go index 2bdacb465ffa..ea82b16a3659 100644 --- a/database/encdb/db.go +++ b/database/encdb/db.go @@ -204,6 +204,7 @@ func (b *batch) Reset() { if cap(b.ops) > len(b.ops)*database.MaxExcessCapacityFactor { b.ops = make([]database.BatchOp, 0, cap(b.ops)/database.CapacityReductionFactor) } else { + clear(b.ops) b.ops = b.ops[:0] } b.Batch.Reset() diff --git a/database/prefixdb/db.go b/database/prefixdb/db.go index b3082d9e986e..e1ec8da5007e 100644 --- a/database/prefixdb/db.go +++ b/database/prefixdb/db.go @@ -313,6 +313,7 @@ func (b *batch) Reset() { if cap(b.ops) > len(b.ops)*database.MaxExcessCapacityFactor { b.ops = make([]batchOp, 0, cap(b.ops)/database.CapacityReductionFactor) } else { + clear(b.ops) b.ops = b.ops[:0] } b.Batch.Reset() diff --git a/utils/set/sampleable_set.go b/utils/set/sampleable_set.go index efb63c2d777c..a70b332a9a68 100644 --- a/utils/set/sampleable_set.go +++ b/utils/set/sampleable_set.go @@ -108,9 +108,7 @@ func (s *SampleableSet[T]) Remove(elements ...T) { // Clear empties this set func (s *SampleableSet[T]) Clear() { clear(s.indices) - for i := range s.elements { - s.elements[i] = utils.Zero[T]() - } + clear(s.elements) s.elements = s.elements[:0] } diff --git a/utils/zero.go b/utils/zero.go index c691ed2e653c..bb31de1c8d58 100644 --- a/utils/zero.go +++ b/utils/zero.go @@ -4,16 +4,6 @@ package utils // Returns a new instance of a T. -func Zero[T any]() T { - return *new(T) -} - -// ZeroSlice sets all values of the provided slice to the type's zero value. -// -// This can be useful to ensure that the garbage collector doesn't hold -// references to values that are no longer desired. -func ZeroSlice[T any](s []T) { - for i := range s { - s[i] = *new(T) - } +func Zero[T any]() (_ T) { + return }