-
Notifications
You must be signed in to change notification settings - Fork 20.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
all: assign zero after resize in implementations of heap.Interface #26296
Conversation
old[n-1] = nil | ||
*s = old[0 : n-1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting idea -- but I think this should not be needed. Instead of nilling the item, we resize the slice.
Taken to the extreme, imagine we have a slice with 100 pointers, and then do myslice = myslice[:0]
-- that should free up all the pointers for the gc, without us explicitly nilling them. It's the same case here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think resizing would help since the same underlying array is referenced.
This is the example for a priority queue from the standard lib:
func (pq *PriorityQueue) Pop() any {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}
@@ -70,6 +70,7 @@ func (h *expHeap) Pop() interface{} { | |||
old := *h | |||
n := len(old) | |||
x := old[n-1] | |||
old[n-1] = expItem{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this is moot, and will be forgotten by the next line
@@ -45,6 +45,7 @@ func (h *nonceHeap) Pop() interface{} { | |||
old := *h | |||
n := len(old) | |||
x := old[n-1] | |||
old[n-1] = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting a uint64
to 0
does not make any difference to the gc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slice tricks from the Go wiki states that you should set the item that is deleted to the zero value of T.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…thereum#26296) This changes the Pop method to assign the zero value before reducing slice size. Doing so ensures the backing array does not reference removed item values.
Set the deleted item to its zero val so it can be garbage collected.