Skip to content

Commit f4afb4f

Browse files
committed
ext4: optimize test_root()
The test_root() function could potentially loop forever due to overflow issues. So rewrite test_root() to avoid this issue; as a bonus, it is 38% faster when benchmarked via a test loop: int main(int argc, char **argv) { int i; for (i = 0; i < 1 << 24; i++) { if (test_root(i, 7)) printf("%d\n", i); } } Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
1 parent 2f2e09e commit f4afb4f

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎fs/ext4/balloc.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,15 @@ ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
682682

683683
static inline int test_root(ext4_group_t a, int b)
684684
{
685-
int num = b;
686-
687-
while (a > num)
688-
num *= b;
689-
return num == a;
685+
while (1) {
686+
if (a < b)
687+
return 0;
688+
if (a == b)
689+
return 1;
690+
if ((a % b) != 0)
691+
return 0;
692+
a = a / b;
693+
}
690694
}
691695

692696
static int ext4_group_sparse(ext4_group_t group)

0 commit comments

Comments
 (0)