add patch fix-duplicate-label-for-phase-2-commit
[ext4-patch-queue.git] / optimize-test_root
blob8f46607b9a26d34276e2a75dddbe1efa3bfc0bb8
1 ext4: optimize test_root()
3 The test_root() function could potentially loop forever due to
4 overflow issues.  So rewrite test_root() to avoid this issue; as a
5 bonus, it is 38% faster when benchmarked via a test loop:
7 int main(int argc, char **argv)
9         int  i;
11         for (i = 0; i < 1 << 24; i++) {
12                 if (test_root(i, 7))
13                         printf("%d\n", i);
14         }
17 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
18 ---
19  fs/ext4/balloc.c | 14 +++++++++-----
20  1 file changed, 9 insertions(+), 5 deletions(-)
22 diff --git a/fs/ext4/balloc.c b/fs/ext4/balloc.c
23 index d0f13ea..5833939 100644
24 --- a/fs/ext4/balloc.c
25 +++ b/fs/ext4/balloc.c
26 @@ -682,11 +682,15 @@ ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
28  static inline int test_root(ext4_group_t a, int b)
29  {
30 -       int num = b;
32 -       while (a > num)
33 -               num *= b;
34 -       return num == a;
35 +       while (1) {
36 +               if (a < b)
37 +                       return 0;
38 +               if (a == b)
39 +                       return 1;
40 +               if ((a % b) != 0)
41 +                       return 0;
42 +               a = a / b;
43 +       }
44  }
46  static int ext4_group_sparse(ext4_group_t group)