Skip to content

Commit

Permalink
Ensure slices are zeroed (#3407)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Sep 20, 2024
1 parent c6d874b commit 75fc684
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 15 deletions.
1 change: 1 addition & 0 deletions database/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (b *BatchOps) Size() int {
}

func (b *BatchOps) Reset() {
clear(b.Ops)
b.Ops = b.Ops[:0]
b.size = 0
}
Expand Down
1 change: 1 addition & 0 deletions database/encdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions database/prefixdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions utils/set/sampleable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
14 changes: 2 additions & 12 deletions utils/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 75fc684

Please sign in to comment.