Skip to content

Commit f8b8c3a

Browse files
committed
Refactor fn Bucket::next
1 parent a619fdd commit f8b8c3a

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

src/libstd/collections/hash/table.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,22 +270,14 @@ impl<K, V, M: Deref<Target=RawTable<K, V>>> Bucket<K, V, M> {
270270

271271
/// Modifies the bucket pointer in place to make it point to the next slot.
272272
pub fn next(&mut self) {
273-
// Branchless bucket iteration step.
274-
// As we reach the end of the table...
275-
// We take the current idx: 0111111b
276-
// Xor it by its increment: ^ 1000000b
277-
// ------------
278-
// 1111111b
279-
// Then AND with the capacity: & 1000000b
280-
// ------------
281-
// to get the backwards offset: 1000000b
282-
// ... and it's zero at all other times.
283-
let maybe_wraparound_dist = (self.idx ^ (self.idx + 1)) & self.table.capacity();
284-
// Finally, we obtain the offset 1 or the offset -cap + 1.
285-
let dist = 1 - (maybe_wraparound_dist as isize);
286-
287273
self.idx += 1;
288-
274+
let range = self.table.capacity();
275+
// This code is branchless thanks to a conditional move.
276+
let dist = if self.idx & (range - 1) == 0 {
277+
1 - range as isize
278+
} else {
279+
1
280+
};
289281
unsafe {
290282
self.raw = self.raw.offset(dist);
291283
}

0 commit comments

Comments
 (0)