mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] blkdev: use an efficient way to check merge flags
@ 2014-04-02 15:19 Jianyu Zhan
  2014-04-02 18:13 ` Jan Kara
  0 siblings, 1 reply; 5+ messages in thread
From: Jianyu Zhan @ 2014-04-02 15:19 UTC (permalink / raw)
  To: axboe; +Cc: hch, jack, viro, kmo, namjae.jeon, nasa4836, linux-kernel

A bitwise flag comparison could be done using a more efficient bit-ops way.

Signed-off-by: Jianyu Zhan <nasa4836@gmail.com>
---
 include/linux/blkdev.h | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 1e1fa3f..adfa40a 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -655,16 +655,9 @@ static inline bool rq_mergeable(struct request *rq)
 static inline bool blk_check_merge_flags(unsigned int flags1,
 					 unsigned int flags2)
 {
-	if ((flags1 & REQ_DISCARD) != (flags2 & REQ_DISCARD))
-		return false;
-
-	if ((flags1 & REQ_SECURE) != (flags2 & REQ_SECURE))
-		return false;
-
-	if ((flags1 & REQ_WRITE_SAME) != (flags2 & REQ_WRITE_SAME))
-		return false;
-
-	return true;
+	return (flags1 & (REQ_DISCARD | REQ_SECURE | REQ_WRITE_SAME)) ^
+	       (flags2 & (REQ_DISCARD | REQ_SECURE | REQ_WRITE_SAME))
+		== 0;
 }
 
 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
-- 
1.9.0.GIT


^ permalink raw reply	[flat|nested] 5+ messages in thread
* Re: [PATCH] blkdev: use an efficient way to check merge flags
@ 2014-04-03 13:37 Jianyu Zhan
  0 siblings, 0 replies; 5+ messages in thread
From: Jianyu Zhan @ 2014-04-03 13:37 UTC (permalink / raw)
  To: jack; +Cc: axboe, linux-kernel, viro, Jianyu Zhan

Hi, Jan,

I've just renewed the patch as you suggusted. Actually it isn't quite 
performance sensitive, but the point is one less branch leads to
less penalty caused by branch prediction failure. Ok, this may be 
way too paranoid.:-)

A bitwise flag comparison could be done using a more efficient bit-ops
way, by mimicking GCC logic of optimizing such bitwise comparison.

Signed-off-by: Jianyu Zhan <nasa4836@gmail.com>
---
 include/linux/blkdev.h | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 1e1fa3f..f2b79fc 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -655,16 +655,18 @@ static inline bool rq_mergeable(struct request *rq)
 static inline bool blk_check_merge_flags(unsigned int flags1,
 					 unsigned int flags2)
 {
-	if ((flags1 & REQ_DISCARD) != (flags2 & REQ_DISCARD))
-		return false;
-
-	if ((flags1 & REQ_SECURE) != (flags2 & REQ_SECURE))
-		return false;
-
-	if ((flags1 & REQ_WRITE_SAME) != (flags2 & REQ_WRITE_SAME))
-		return false;
-
-	return true;
+	/*
+	 * Check whether all tree flags are the same in both
+	 * flags.
+	 *
+	 * Replace original three-if's comparision with a
+	 * more efficient method, by mimicking the GCC logic of
+	 * optimizing such bitwise comparion. This makes GCC
+	 * to spit out most compact and least brach code.
+	 */
+	return ((flags1 ^ flags2) &
+		(REQ_DISCARD | REQ_SECURE | REQ_WRITE_SAME))
+		== 0;
 }
 
 static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
-- 
1.9.0.GIT


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-04-03 13:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-02 15:19 [PATCH] blkdev: use an efficient way to check merge flags Jianyu Zhan
2014-04-02 18:13 ` Jan Kara
2014-04-03  8:00   ` Zhan Jianyu
2014-04-03 12:45     ` Jan Kara
2014-04-03 13:37 Jianyu Zhan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®