Skip to content
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

Merged
merged 1 commit into from
Dec 5, 2022

Conversation

estensen
Copy link
Contributor

@estensen estensen commented Dec 3, 2022

Set the deleted item to its zero val so it can be garbage collected.

Comment on lines +511 to 512
old[n-1] = nil
*s = old[0 : n-1]
Copy link
Contributor

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.

Copy link
Contributor Author

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
}

https://pkg.go.dev/container/heap

@@ -70,6 +70,7 @@ func (h *expHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
old[n-1] = expItem{}
Copy link
Contributor

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
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting!

Copy link
Contributor

@holiman holiman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, curious what @fjl or @karalabe thinks

@fjl fjl changed the title core, p2p: avoiding leaking the popped item all: assign zero after resize in implementations of heap.Interface Dec 5, 2022
@fjl fjl merged commit 06632da into ethereum:master Dec 5, 2022
@fjl fjl added this to the 1.11.0 milestone Dec 5, 2022
@estensen estensen deleted the pop-leak branch December 5, 2022 12:58
shekhirin pushed a commit to shekhirin/go-ethereum that referenced this pull request Jun 6, 2023
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants