fs/ufs: optimize ufs_set_de_type() with lookup table
PATCH 1/1 fs/ufs: optimize ufs_set_de_type() with lookup table
From: Shaurya Rane <[email protected]>
Replace the switch statement in ufs_set_de_type() with a static lookup table as suggested by the existing TODO comment.
This optimization converts the case-by-case comparisons into a simple array access. The implementation relies on the S_IFMT file type constants having predictable bit patterns residing in the high bits (12-15).
By masking the mode and shifting right by 12 bits ((mode & S_IFMT) >> 12), we obtain compact indices (0-15) that map directly to the DT_ constants:
S_IFIFO (0010000) -> index 1 -> DT_FIFO S_IFCHR (0020000) -> index 2 -> DT_CHR S_IFDIR (0040000) -> index 4 -> DT_DIR S_IFBLK (0060000) -> index 6 -> DT_BLK S_IFREG (0100000) -> index 8 -> DT_REG S_IFLNK (0120000) -> index 10 -> DT_LNK
S_IFSOCK (0140000) -> index 12 -> DT_SOCK
All other indices map to DT_UNKNOWN (0), maintaining identical logic to the original default case.
Signed-off-by Shaurya Rane <[email protected]>
--- fs/ufs/util.h | 48 ++++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 28 deletions(-)
fs/ufs/util.h +20 -28
@@ -149,37 +149,29 @@ ufs_set_de_namlen(struct super_block *sb, struct ufs_dir_entry *de, u16 value)static inline void ufs_set_de_type (struct super_block *sb,struct ufs_dir_entry *de,int mode) { +static const unsigned char ifmt_to_dt[16 ] = { + [0 ] =DT_UNKNOWN , + [1 ] =DT_FIFO , + [2 ] =DT_CHR , + [3 ] =DT_UNKNOWN , + [4 ] =DT_DIR , + [5 ] =DT_UNKNOWN , + [6 ] =DT_BLK , + [7 ] =DT_UNKNOWN , + [8 ] =DT_REG , + [9 ] =DT_UNKNOWN , + [10 ] =DT_LNK , + [11 ] =DT_UNKNOWN , + [12 ] =DT_SOCK , + [13 ] =DT_UNKNOWN , + [14 ] =DT_UNKNOWN , + [15 ] =DT_UNKNOWN , + }; +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 ; - } + de->d_u.d_44.d_type = ifmt_to_dt[(mode &S_IFMT ) >>12 ]; } static inline u32
--
2.34.1