From: Sami Liedes Date: Thu, 22 Mar 2012 23:42:38 +0000 (-0400) Subject: libext2fs: move a modulo operation out of a hot loop. X-Git-Tag: v1.42.2~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75556776d315e67e411b983cdcdb05eb74b803a9;p=thirdparty%2Fe2fsprogs.git libext2fs: move a modulo operation out of a hot loop. Filesystem shrinking in particular is a heavy user of this loop in ext2fs_new_inode(). This change makes resize2fs use 24% less CPU time for shrinking a 100G filesystem. Signed-off-by: Sami Liedes Signed-off-by: Theodore Ts'o --- diff --git a/lib/ext2fs/alloc.c b/lib/ext2fs/alloc.c index 948a0ecba..bcdc2d446 100644 --- a/lib/ext2fs/alloc.c +++ b/lib/ext2fs/alloc.c @@ -109,6 +109,7 @@ errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir, ext2_ino_t dir_group = 0; ext2_ino_t i; ext2_ino_t start_inode; + ext2_ino_t ino_in_group; EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); @@ -126,17 +127,22 @@ errcode_t ext2fs_new_inode(ext2_filsys fs, ext2_ino_t dir, if (start_inode > fs->super->s_inodes_count) return EXT2_ET_INODE_ALLOC_FAIL; i = start_inode; + ino_in_group = (i - 1) % EXT2_INODES_PER_GROUP(fs->super); do { - if (((i - 1) % EXT2_INODES_PER_GROUP(fs->super)) == 0) + if (ino_in_group == 0) check_inode_uninit(fs, map, (i - 1) / EXT2_INODES_PER_GROUP(fs->super)); if (!ext2fs_fast_test_inode_bitmap2(map, i)) break; - i++; - if (i > fs->super->s_inodes_count) + if (++ino_in_group == EXT2_INODES_PER_GROUP(fs->super)) + ino_in_group = 0; + if (++i > fs->super->s_inodes_count) { i = EXT2_FIRST_INODE(fs->super); + ino_in_group = ((i - 1) % + EXT2_INODES_PER_GROUP(fs->super)); + } } while (i != start_inode); if (ext2fs_test_inode_bitmap2(map, i))