Skip to content

Commit

Permalink
ext4: optimize test_root()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Theodore Ts'o committed Jun 6, 2013
1 parent 2f2e09e commit f4afb4f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions fs/ext4/balloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,15 @@ ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)

static inline int test_root(ext4_group_t a, int b)
{
int num = b;

while (a > num)
num *= b;
return num == a;
while (1) {
if (a < b)
return 0;
if (a == b)
return 1;
if ((a % b) != 0)
return 0;
a = a / b;
}
}

static int ext4_group_sparse(ext4_group_t group)
Expand Down

0 comments on commit f4afb4f

Please sign in to comment.