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
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,15 @@ impl Iterator for ThreadSetIterator {
if self.0 == 0 {
None
} else {
// Find the first set bit by counting trailing zeros.
// This is guaranteed to be < 64 because self.0 != 0.
let thread_id = self.0.trailing_zeros() as ThreadId;
// Clear the lowest set bit. The subtraction is safe because
// we know that self.0 != 0.
// 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.

Some(thread_id)
}
Expand Down
Loading