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

p2p/discover: improve nodesByDistance.push #26019

Merged
merged 2 commits into from
Dec 7, 2022

Conversation

zhiqiangxu
Copy link
Contributor

@zhiqiangxu zhiqiangxu commented Oct 21, 2022

Spotted this days ago, today I was writing something similar and I decide to create a PR for this.

It just doesn't feel right to check ix == len(h.entries) after possibly appending an additional entry to h.entries.

@fjl correct me if there's something missed out:)

@fjl
Copy link
Contributor

fjl commented Oct 21, 2022

I don't get it. Is there a bug in the current version?

@zhiqiangxu
Copy link
Contributor Author

zhiqiangxu commented Oct 21, 2022

I don't get it. Is there a bug in the current version?

No bug here, but I think the comment is not accurate, multiple cases are coerced into one. For example, in the else branch, it could happen that no append happened.

@holiman
Copy link
Contributor

holiman commented Oct 21, 2022

I think the change is harder to understand than before the change.

@zhiqiangxu
Copy link
Contributor Author

zhiqiangxu commented Oct 21, 2022

I think the change is harder to understand than before the change.

sort.Search(N, ) will at most return N when no match, so ix should compare with N, not N+1. The current version sometimes compare ix with N+1. But in the end, the final result is the same... I'd say the reduction involved is not that easy to follow.

@fjl fjl changed the title p2p/discover: fix nodesByDistance.push p2p/discover: improve nodesByDistance.push Dec 7, 2022
@fjl fjl merged commit a9dfac0 into ethereum:master Dec 7, 2022
@fjl fjl added this to the 1.11.0 milestone Dec 7, 2022
shekhirin pushed a commit to shekhirin/go-ethereum that referenced this pull request Jun 6, 2023
This improves readability of function 'push'.

sort.Search(N, ...) will at most return N when no match, so ix should be compared
with N. The previous version would compare ix with N+1 in case an additional item
was appended. No bug resulted from this comparison, but it's not easy to understand
why.

Co-authored-by: Felix Lange <[email protected]>
nibty pushed a commit to FairCrypto/go-ethereum that referenced this pull request Apr 10, 2024
This improves readability of function 'push'.

sort.Search(N, ...) will at most return N when no match, so ix should be compared
with N. The previous version would compare ix with N+1 in case an additional item
was appended. No bug resulted from this comparison, but it's not easy to understand
why.

Co-authored-by: Felix Lange <[email protected]>
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