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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/txpool/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!

*h = old[0 : n-1]
return x
}
Expand Down
1 change: 1 addition & 0 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ func (s *TxByPriceAndTime) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
old[n-1] = nil
*s = old[0 : n-1]
Comment on lines +511 to 512
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

return x
}
Expand Down
1 change: 1 addition & 0 deletions p2p/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

*h = old[0 : n-1]
return x
}