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

ThreadSetIterator::next comment #4006

Merged
merged 1 commit into from
Dec 7, 2024

Conversation

apfitzge
Copy link

@apfitzge apfitzge commented Dec 7, 2024

Problem

  • self.0 &= self.0 - 1, while common, is admittedly not that clear

Summary of Changes

  • Add some comments to ThreadSetIterator::next to make it clear why this works

Fixes #

// Example (with 4 bits):
// self.0 = 0b1010 // initial value
// self.0 - 1 = 0b1001 // all bits at or after the lowest set bit are flipped
// 0b1010 & 0b1001 = 0b1000 // the lowest bit has been cleared
self.0 &= self.0 - 1;
Copy link
Author

Choose a reason for hiding this comment

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

Could expand comment to state that this is possibly faster than the slightly more clear bit-clearing:

self.0 ^= 1 << thread_id;

but comes down to instruction pipelining since self.0-1 doesn't depend sequentially on thread_id. And I think probably more important to just document why this works in the first place.

Copy link

@t-nelson t-nelson left a comment

Choose a reason for hiding this comment

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

perfect! thanks

@apfitzge apfitzge merged commit 8e6b2f8 into anza-xyz:master Dec 7, 2024
40 checks passed
@apfitzge apfitzge deleted the bit_fiddling_comment branch December 7, 2024 18:19
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.

2 participants