fs/ufs: refactor ufs_set_de_type in util.h to use a table lookup

Даниил Булгарь posted 1 patch 11 hours ago
There is a newer version of this series
fs/ufs/util.h | 39 +++++++++++----------------------------
1 file changed, 11 insertions(+), 28 deletions(-)
fs/ufs: refactor ufs_set_de_type in util.h to use a table lookup
Posted by Даниил Булгарь 11 hours ago
fs/ufs: refactor ufs_set_de_type in util.h to use a table lookup

Replace the switch-case logic with a more efficient table lookup for
converting mode bits to directory entry types.
This addresses an explicit TODO in the code.
The change reduces code complexity and slightly decreases the
compiled size of the function by replacing
branching logic with a constant 16-byte array.

Signed-off-by: Daniil Bulgar <bulgardaniil18@gmail.com>
---
 fs/ufs/util.h | 39 +++++++++++----------------------------
 1 file changed, 11 insertions(+), 28 deletions(-)

diff --git a/fs/ufs/util.h b/fs/ufs/util.h
index 391bb4f11..8966c3ab5 100644
--- a/fs/ufs/util.h
+++ b/fs/ufs/util.h
@@ -152,34 +152,17 @@ ufs_set_de_type(struct super_block *sb, struct
ufs_dir_entry *de, int mode)
     if ((UFS_SB(sb)->s_flags & UFS_DE_MASK) != UFS_DE_44BSD)
         return;

-    /*
-    * TODO turn this into a table lookup
-    */
-    switch (mode & S_IFMT) {
-    case S_IFSOCK:
-        de->d_u.d_44.d_type = DT_SOCK;
-        break;
-    case S_IFLNK:
-        de->d_u.d_44.d_type = DT_LNK;
-        break;
-    case S_IFREG:
-        de->d_u.d_44.d_type = DT_REG;
-        break;
-    case S_IFBLK:
-        de->d_u.d_44.d_type = DT_BLK;
-        break;
-    case S_IFDIR:
-        de->d_u.d_44.d_type = DT_DIR;
-        break;
-    case S_IFCHR:
-        de->d_u.d_44.d_type = DT_CHR;
-        break;
-    case S_IFIFO:
-        de->d_u.d_44.d_type = DT_FIFO;
-        break;
-    default:
-        de->d_u.d_44.d_type = DT_UNKNOWN;
-    }
+    static const unsigned char type_table[16] = {
+        [S_IFSOCK >> 12]    = DT_SOCK,
+        [S_IFLNK >> 12]        = DT_LNK,
+        [S_IFREG >> 12]        = DT_REG,
+        [S_IFBLK >> 12]        = DT_BLK,
+        [S_IFDIR >> 12]        = DT_DIR,
+        [S_IFCHR >> 12]        = DT_CHR,
+        [S_IFIFO >> 12]        = DT_FIFO,
+    };
+
+    de->d_u.d_44.d_type = type_table[(mode & S_IFMT) >> 12];
 }

 static inline u32
-- 
2.53.0