Whamcloud - gitweb
libext2fs: move a modulo operation out of a hot loop.
authorSami Liedes <sami.liedes@iki.fi>
Thu, 22 Mar 2012 23:42:38 +0000 (19:42 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 22 Mar 2012 23:43:48 +0000 (19:43 -0400)
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 <sami.liedes@iki.fi>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/alloc.c

index 948a0ec..bcdc2d4 100644 (file)
@@ -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))