rockskip: ruler function is just bits.TrailingZeros (#62567)

I couldn't help but make this change after seeing the algorithm. Feel
free to just close this if this makes it less clear, but this kinda
feels clearer in my head but I am probably weird.

Test Plan: TestRuler passes
This commit is contained in:
Keegan Carruthers-Smith 2024-05-13 17:55:43 +02:00 committed by GitHub
parent 68fe349b01
commit 1515512ec2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@ package rockskip
import (
"context"
"database/sql"
"math/bits"
"sync"
"time"
@ -168,10 +169,11 @@ func getHops(ctx context.Context, tx dbutil.DB, commit int, tasklog *TaskLog) ([
//
// https://oeis.org/A007814
func ruler(n int) int {
height := 0
for n > 0 && n%2 == 0 {
height++
n = n / 2
if n <= 0 {
return 0
}
return height
// ruler(n) is equivalent to asking how many times can you divide n by 2
// before you get an odd number. That is the number of 0's at the end of n
// when n is written in base 2.
return bits.TrailingZeros(uint(n))
}