Skip to content

Commit cfb271d

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: add unlikely() macro for compiler optimization
As we know, some of our branch condition will rarely be true. So we could add 'unlikely' to let compiler optimize these code, by this way we could drop unneeded 'jump' assemble code to improve performance. change log: o add *unlikely* as many as possible across the whole source files at once suggested by Jaegeuk Kim. Suggested-by: Jaegeuk Kim <jaegeuk.kim@samsung.com> Signed-off-by: Chao Yu <chao2.yu@samsung.com> Signed-off-by: Jaegeuk Kim <jaegeuk.kim@samsung.com>
1 parent b9987a2 commit cfb271d

6 files changed

Lines changed: 33 additions & 27 deletions

File tree

‎fs/f2fs/checkpoint.c‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,25 @@ static int f2fs_write_meta_page(struct page *page,
8282
struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
8383

8484
/* Should not write any meta pages, if any IO error was occurred */
85-
if (wbc->for_reclaim || sbi->por_doing ||
86-
is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG)) {
87-
dec_page_count(sbi, F2FS_DIRTY_META);
88-
wbc->pages_skipped++;
89-
set_page_dirty(page);
90-
return AOP_WRITEPAGE_ACTIVATE;
91-
}
85+
if (unlikely(sbi->por_doing ||
86+
is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ERROR_FLAG)))
87+
goto redirty_out;
88+
89+
if (wbc->for_reclaim)
90+
goto redirty_out;
9291

9392
wait_on_page_writeback(page);
9493

9594
write_meta_page(sbi, page);
9695
dec_page_count(sbi, F2FS_DIRTY_META);
9796
unlock_page(page);
9897
return 0;
98+
99+
redirty_out:
100+
dec_page_count(sbi, F2FS_DIRTY_META);
101+
wbc->pages_skipped++;
102+
set_page_dirty(page);
103+
return AOP_WRITEPAGE_ACTIVATE;
99104
}
100105

101106
static int f2fs_write_meta_pages(struct address_space *mapping,
@@ -137,7 +142,7 @@ long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
137142
nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
138143
PAGECACHE_TAG_DIRTY,
139144
min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
140-
if (nr_pages == 0)
145+
if (unlikely(nr_pages == 0))
141146
break;
142147

143148
for (i = 0; i < nr_pages; i++) {
@@ -150,7 +155,8 @@ long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
150155
unlock_page(page);
151156
break;
152157
}
153-
if (nwritten++ >= nr_to_write)
158+
nwritten++;
159+
if (unlikely(nwritten >= nr_to_write))
154160
break;
155161
}
156162
pagevec_release(&pvec);
@@ -200,7 +206,7 @@ int acquire_orphan_inode(struct f2fs_sb_info *sbi)
200206
max_orphans = (sbi->blocks_per_seg - 2 - NR_CURSEG_TYPE)
201207
* F2FS_ORPHANS_PER_BLOCK;
202208
mutex_lock(&sbi->orphan_inode_mutex);
203-
if (sbi->n_orphans >= max_orphans)
209+
if (unlikely(sbi->n_orphans >= max_orphans))
204210
err = -ENOSPC;
205211
else
206212
sbi->n_orphans++;

‎fs/f2fs/data.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ int reserve_new_block(struct dnode_of_data *dn)
251251

252252
if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
253253
return -EPERM;
254-
if (!inc_valid_block_count(sbi, dn->inode, 1))
254+
if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
255255
return -ENOSPC;
256256

257257
trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
@@ -711,7 +711,7 @@ static int f2fs_write_data_page(struct page *page,
711711

712712
zero_user_segment(page, offset, PAGE_CACHE_SIZE);
713713
write:
714-
if (sbi->por_doing) {
714+
if (unlikely(sbi->por_doing)) {
715715
err = AOP_WRITEPAGE_ACTIVATE;
716716
goto redirty_out;
717717
}

‎fs/f2fs/dir.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
190190
unsigned int max_depth;
191191
unsigned int level;
192192

193-
if (namelen > F2FS_NAME_LEN)
193+
if (unlikely(namelen > F2FS_NAME_LEN))
194194
return NULL;
195195

196196
if (npages == 0)
@@ -461,7 +461,7 @@ int __f2fs_add_link(struct inode *dir, const struct qstr *name, struct inode *in
461461
}
462462

463463
start:
464-
if (current_depth == MAX_DIR_HASH_DEPTH)
464+
if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
465465
return -ENOSPC;
466466

467467
/* Increase the depth, if required */

‎fs/f2fs/f2fs.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
574574
static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
575575
{
576576
WARN_ON((nid >= NM_I(sbi)->max_nid));
577-
if (nid >= NM_I(sbi)->max_nid)
577+
if (unlikely(nid >= NM_I(sbi)->max_nid))
578578
return -EINVAL;
579579
return 0;
580580
}
@@ -600,7 +600,7 @@ static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
600600
spin_lock(&sbi->stat_lock);
601601
valid_block_count =
602602
sbi->total_valid_block_count + (block_t)count;
603-
if (valid_block_count > sbi->user_block_count) {
603+
if (unlikely(valid_block_count > sbi->user_block_count)) {
604604
spin_unlock(&sbi->stat_lock);
605605
return false;
606606
}
@@ -719,13 +719,13 @@ static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
719719
spin_lock(&sbi->stat_lock);
720720

721721
valid_block_count = sbi->total_valid_block_count + 1;
722-
if (valid_block_count > sbi->user_block_count) {
722+
if (unlikely(valid_block_count > sbi->user_block_count)) {
723723
spin_unlock(&sbi->stat_lock);
724724
return false;
725725
}
726726

727727
valid_node_count = sbi->total_valid_node_count + 1;
728-
if (valid_node_count > sbi->total_node_count) {
728+
if (unlikely(valid_node_count > sbi->total_node_count)) {
729729
spin_unlock(&sbi->stat_lock);
730730
return false;
731731
}

‎fs/f2fs/node.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static void ra_nat_pages(struct f2fs_sb_info *sbi, int nid)
9494
int i;
9595

9696
for (i = 0; i < FREE_NID_PAGES; i++, nid += NAT_ENTRY_PER_BLOCK) {
97-
if (nid >= nm_i->max_nid)
97+
if (unlikely(nid >= nm_i->max_nid))
9898
nid = 0;
9999
index = current_nat_addr(sbi, nid);
100100

@@ -1160,7 +1160,7 @@ int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino)
11601160
struct page *page = pvec.pages[i];
11611161

11621162
/* until radix tree lookup accepts end_index */
1163-
if (page->index > end)
1163+
if (unlikely(page->index > end))
11641164
continue;
11651165

11661166
if (ino && ino_of_node(page) == ino) {
@@ -1190,7 +1190,7 @@ static int f2fs_write_node_page(struct page *page,
11901190
block_t new_addr;
11911191
struct node_info ni;
11921192

1193-
if (sbi->por_doing)
1193+
if (unlikely(sbi->por_doing))
11941194
goto redirty_out;
11951195

11961196
wait_on_page_writeback(page);
@@ -1326,7 +1326,7 @@ static int add_free_nid(struct f2fs_nm_info *nm_i, nid_t nid, bool build)
13261326
return -1;
13271327

13281328
/* 0 nid should not be used */
1329-
if (nid == 0)
1329+
if (unlikely(nid == 0))
13301330
return 0;
13311331

13321332
if (build) {
@@ -1379,7 +1379,7 @@ static void scan_nat_page(struct f2fs_nm_info *nm_i,
13791379

13801380
for (; i < NAT_ENTRY_PER_BLOCK; i++, start_nid++) {
13811381

1382-
if (start_nid >= nm_i->max_nid)
1382+
if (unlikely(start_nid >= nm_i->max_nid))
13831383
break;
13841384

13851385
blk_addr = le32_to_cpu(nat_blk->entries[i].block_addr);
@@ -1413,7 +1413,7 @@ static void build_free_nids(struct f2fs_sb_info *sbi)
14131413
f2fs_put_page(page, 1);
14141414

14151415
nid += (NAT_ENTRY_PER_BLOCK - (nid % NAT_ENTRY_PER_BLOCK));
1416-
if (nid >= nm_i->max_nid)
1416+
if (unlikely(nid >= nm_i->max_nid))
14171417
nid = 0;
14181418

14191419
if (i++ == FREE_NID_PAGES)
@@ -1447,7 +1447,7 @@ bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid)
14471447
struct free_nid *i = NULL;
14481448
struct list_head *this;
14491449
retry:
1450-
if (sbi->total_valid_node_count + 1 >= nm_i->max_nid)
1450+
if (unlikely(sbi->total_valid_node_count + 1 >= nm_i->max_nid))
14511451
return false;
14521452

14531453
spin_lock(&nm_i->free_nid_list_lock);
@@ -1557,7 +1557,7 @@ int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page)
15571557
new_ni = old_ni;
15581558
new_ni.ino = ino;
15591559

1560-
if (!inc_valid_node_count(sbi, NULL))
1560+
if (unlikely(!inc_valid_node_count(sbi, NULL)))
15611561
WARN_ON(1);
15621562
set_node_addr(sbi, &new_ni, NEW_ADDR);
15631563
inc_valid_inode_count(sbi);

‎fs/f2fs/segment.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi, int freed)
457457
int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
458458
int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
459459

460-
if (sbi->por_doing)
460+
if (unlikely(sbi->por_doing))
461461
return false;
462462

463463
return ((free_sections(sbi) + freed) <= (node_secs + 2 * dent_secs +

0 commit comments

Comments
 (0)