Skip to contentSkip to conversation

bcachefs: eytzinger1_{next,prev} cleanup

PATCH In review Andreas Gruenbacher 2 participants 4 messages Nov 24, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bcachefs: eytzinger1_{next,prev} cleanup

From: Andreas Gruenbacher <[email protected]>

In this place in the code, eytzinger1_next() is trying to move the lowest child on the left, which is equivalent to doubling i until the next doubling would cause it to be greater than size. This is implemented by shifting i to the left so that the most significant bits of both numbers align, and then shifting i right by one if it is greater than size.

The code accidentally computes the most significant bit of (size + 1) instead of size. This happens to be harmless because in that case, the right shifting will compensate, but it is confusing and unnecessary.

Likewise, eytzinger1_prev() is trying to move the lowest child on the right. The same applies here.

---
 fs/bcachefs/eytzinger.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bcachefs/eytzinger.h +2 -2
index 0541192d7bc0..9fa63a624c92 100644
@@ -73,7 +73,7 @@ static inline unsigned eytzinger1_next(unsigned i, unsigned size)
 	if (eytzinger1_right_child(i) <= size) {
 		i = eytzinger1_right_child(i);
 
-		i <<= __fls(size + 1) - __fls(i);
+		i <<= __fls(size) - __fls(i);
 		i >>= i > size;
 	} else {
 		i >>= ffz(i) + 1;
@@ -89,7 +89,7 @@ static inline unsigned eytzinger1_prev(unsigned i, unsigned size)
 	if (eytzinger1_left_child(i) <= size) {
 		i = eytzinger1_left_child(i) + 1;
 
-		i <<= __fls(size + 1) - __fls(i);
+		i <<= __fls(size) - __fls(i);
 		i -= 1;
 		i >>= i > size;
 	} else {
Signature · 1 line
2.46.2
#1 lore raw
Quoted 9 lines · Andreas Gruenbacher

From: Andreas Gruenbacher <[email protected]>

In this place in the code, eytzinger1_next() is trying to move the lowest child on the left, which is equivalent to doubling i until the next doubling would cause it to be greater than size. This is implemented by shifting i to the left so that the most significant bits of both numbers align, and then shifting i right by one if it is greater than size.

Quoted 8 lines

The code accidentally computes the most significant bit of (size + 1) instead of size. This happens to be harmless because in that case, the right shifting will compensate, but it is confusing and unnecessary.

Likewise, eytzinger1_prev() is trying to move the lowest child on the right. The same applies here.

Looks correct. There's eytzinger tests in fs/bcachefs/util.c, could you run those?

If you're doing any more work with the eytzinger code we could also turn those tests into more standard unit tests, right now there's no harness to drive them.

Quoted 29 lines
---
 fs/bcachefs/eytzinger.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/bcachefs/eytzinger.h +2 -2
index 0541192d7bc0..9fa63a624c92 100644
@@ -73,7 +73,7 @@ static inline unsigned eytzinger1_next(unsigned i, unsigned size)
 	if (eytzinger1_right_child(i) <= size) {
 		i = eytzinger1_right_child(i);
 
-		i <<= __fls(size + 1) - __fls(i);
+		i <<= __fls(size) - __fls(i);
 		i >>= i > size;
 	} else {
 		i >>= ffz(i) + 1;
@@ -89,7 +89,7 @@ static inline unsigned eytzinger1_prev(unsigned i, unsigned size)
 	if (eytzinger1_left_child(i) <= size) {
 		i = eytzinger1_left_child(i) + 1;
 
-		i <<= __fls(size + 1) - __fls(i);
+		i <<= __fls(size) - __fls(i);
 		i -= 1;
 		i >>= i > size;
 	} else {
Signature · 1 line
2.46.2
#2 lore raw
Quoted 14 lines · Kent Overstreet
Quoted 13 lines · Andreas Gruenbacher

From: Andreas Gruenbacher <[email protected]>

In this place in the code, eytzinger1_next() is trying to move the lowest child on the left, which is equivalent to doubling i until the next doubling would cause it to be greater than size. This is implemented by shifting i to the left so that the most significant bits of both numbers align, and then shifting i right by one if it is greater than size.

The code accidentally computes the most significant bit of (size + 1) instead of size. This happens to be harmless because in that case, the right shifting will compensate, but it is confusing and unnecessary.

Quoted 7 lines
Quoted 4 lines

Likewise, eytzinger1_prev() is trying to move the lowest child on the right. The same applies here.

Looks correct. There's eytzinger tests in fs/bcachefs/util.c, could you run those?

Hmm, this actually breaks the following tests in util.c:

BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size)); BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));

The inverse tests are fine:

BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size)); BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));

And so are all the other tests.

Are those identities actually relevant, or are they just a peculiarity? Do they justify an additional addition in eytzinger1_prev()?

Note that these tests rely on the behavior of __fls(0), which is undefined. On x86_64, __fls(0) returns (long unsigned int)-1.

Thanks,
Andreas

#3 lore raw
Quoted 31 lines · Andreas Gruenbacher
Quoted 14 lines · Kent Overstreet
Quoted 13 lines · Andreas Gruenbacher

From: Andreas Gruenbacher <[email protected]>

In this place in the code, eytzinger1_next() is trying to move the lowest child on the left, which is equivalent to doubling i until the next doubling would cause it to be greater than size. This is implemented by shifting i to the left so that the most significant bits of both numbers align, and then shifting i right by one if it is greater than size.

The code accidentally computes the most significant bit of (size + 1) instead of size. This happens to be harmless because in that case, the right shifting will compensate, but it is confusing and unnecessary.

Quoted 7 lines
Quoted 4 lines

Likewise, eytzinger1_prev() is trying to move the lowest child on the right. The same applies here.

Looks correct. There's eytzinger tests in fs/bcachefs/util.c, could you run those?

Hmm, this actually breaks the following tests in util.c:

BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size)); BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));

The inverse tests are fine:

Quoted 8 lines

BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size)); BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));

And so are all the other tests.

Are those identities actually relevant, or are they just a peculiarity? Do they justify an additional addition in eytzinger1_prev()?

I think they can be considered a peculiarity we can drop, since 0 isn't a valid element for eytzinger1, and the same for -1 and eytzinger0.

Quoted 2 lines

Note that these tests rely on the behavior of __fls(0), which is undefined. On x86_64, __fls(0) returns (long unsigned int)-1.

That's another point in favor of dropping them.

We'll need to be careful with that and do some code auditing or add new debug asserts, since there are other identities we rely on (e.g. for eytzinger_for_each() and likely elsewhere).