Skip to contentSkip to conversation

erofs-utils: lib: avoid unnecessary modulo in cache.c

PATCH New Gao Xiang 1 participant 1 message Sep 14, 2023 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: lib: avoid unnecessary modulo in cache.c

Previously, EROFS_BLKSIZ was a constant so it doesn't matter to use % operator. Let's convert the remaining ones now.

---
 lib/cache.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
lib/cache.c +6 -4
index 5205d57..ea80a10 100644
@@ -63,7 +63,8 @@ static void erofs_bupdate_mapped(struct erofs_buffer_block *bb)
 	if (bb->blkaddr == NULL_ADDR)
 		return;
 
-	bkt = mapped_buckets[bb->type] + bb->buffers.off % erofs_blksiz(&sbi);
+	bkt = mapped_buckets[bb->type] +
+		(bb->buffers.off & (erofs_blksiz(&sbi) - 1));
 	list_del(&bb->mapped_list);
 	list_add_tail(&bb->mapped_list, bkt);
 }
@@ -77,8 +78,9 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 			   bool dryrun)
 {
 	const unsigned int blksiz = erofs_blksiz(&sbi);
+	const unsigned int blkmask = blksiz - 1;
 	const erofs_off_t alignedoffset = roundup(bb->buffers.off, alignsize);
-	const int oob = cmpsgn(roundup((bb->buffers.off - 1) % blksiz + 1,
+	const int oob = cmpsgn(roundup(((bb->buffers.off - 1) & blkmask) + 1,
 				       alignsize) + incr + extrasize, blksiz);
 	bool tailupdate = false;
 	erofs_blk_t blkaddr;
@@ -110,7 +112,7 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 					DIV_ROUND_UP(bb->buffers.off, blksiz);
 		erofs_bupdate_mapped(bb);
 	}
-	return ((alignedoffset + incr - 1) & (blksiz - 1)) + 1;
+	return ((alignedoffset + incr - 1) & blkmask) + 1;
 }
 
 int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr)
@@ -170,7 +172,7 @@ static int erofs_bfind_for_attach(int type, erofs_off_t size,
 
 		DBG_BUGON(cur->type != type);
 		DBG_BUGON(cur->blkaddr == NULL_ADDR);
-		DBG_BUGON(used_before != cur->buffers.off % blksiz);
+		DBG_BUGON(used_before != (cur->buffers.off & (blksiz - 1)));
 
 		ret = __erofs_battach(cur, NULL, size, alignsize,
 				      required_ext + inline_ext, true);
Signature · 1 line
2.39.3