Skip to contentSkip to conversation

blkdev: use an efficient way to check merge flags

PATCH In review Jianyu Zhan 2 participants 4 messages Apr 2, 2014 – Apr 3, 2014 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blkdev: use an efficient way to check merge flags

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

---
 include/linux/blkdev.h | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
include/linux/blkdev.h +3 -10
index 1e1fa3f..adfa40a 100644
@@ -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)
Signature · 1 line
1.9.0.GIT
#1 lore raw
Quoted 1 line · Jianyu Zhan

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

OK, but have you checked the generated code is actually any better? This
is something I'd expect a compiler might be able to optimize anyway. And the original code looks more readable to me.

Honza

Quoted 32 lines
---
 include/linux/blkdev.h | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
include/linux/blkdev.h +3 -10
index 1e1fa3f..adfa40a 100644
@@ -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)
Signature · 1 line
1.9.0.GIT
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#2 lore raw
Quoted 3 lines · Jan Kara

OK, but have you checked the generated code is actually any better? This
is something I'd expect a compiler might be able to optimize anyway. And the original code looks more readable to me.

Hi, Jan,

I've disassemble the code on my x86_64 box (it's inline though, I just look at its call site), and found that this patch DOES make it more efficient.

Orig asm snippt with
patch asm snippt
============ ================

mov    %edx,%ecx                                          mov    %rdx,%r9
xor    %r8d,%ecx                                             xor    %r8d,%r8d
test   $0x80,%cl                                              and    $0x380,%r9d
jne    14c5 <blk_rq_merge_ok+0x15>                test   $0x380,%ecx
and    $0x3,%ch                                              sete   %r8b
jne    14c5 <blk_rq_merge_ok+0x15>                cmp    %r8,%r9

je 14b5 <blk_rq_merge_ok+0x15>

This saves a branch.

Furthermore, I found that gcc is smart enough to try to optimize the code, so if we do
like this, it will generate the most optimal and smallest code :

static inline bool blk_check_merge_flags(unsigned int flags1,
¦unsigned int flags2)

{
        return ((flags1 ^ flags2) &
                (REQ_DISCARD | REQ_SECURE | REQ_WRITE_SAME))
                == 0;
}

this gives out :

mov    %edx,%r8d
xor    %ecx,%r8d
and    $0x380,%r8d
jne    14a5 <blk_rq_merge_ok+0x15>

But yes, it compromises readibility.

Regards,
Jianyu Zhan

#3 lore raw
Quoted 39 lines · Zhan Jianyu
Quoted 3 lines · Jan Kara

OK, but have you checked the generated code is actually any better? This
is something I'd expect a compiler might be able to optimize anyway. And the original code looks more readable to me.

Hi, Jan,

I've disassemble the code on my x86_64 box (it's inline though, I just look at its call site), and found that this patch DOES make it more efficient.

Orig asm snippt with
patch asm snippt
============ ================

mov    %edx,%ecx                                          mov    %rdx,%r9
xor    %r8d,%ecx                                             xor    %r8d,%r8d
test   $0x80,%cl                                              and    $0x380,%r9d
jne    14c5 <blk_rq_merge_ok+0x15>                test   $0x380,%ecx
and    $0x3,%ch                                              sete   %r8b
jne    14c5 <blk_rq_merge_ok+0x15>                cmp    %r8,%r9

je 14b5 <blk_rq_merge_ok+0x15>

This saves a branch.

Furthermore, I found that gcc is smart enough to try to optimize the code, so if we do
like this, it will generate the most optimal and smallest code :

static inline bool blk_check_merge_flags(unsigned int flags1,
¦unsigned int flags2)

{
        return ((flags1 ^ flags2) &
                (REQ_DISCARD | REQ_SECURE | REQ_WRITE_SAME))
                == 0;
}
Quoted 8 lines

this gives out :

mov    %edx,%r8d
xor    %ecx,%r8d
and    $0x380,%r8d
jne    14a5 <blk_rq_merge_ok+0x15>

But yes, it compromises readibility.

OK, I'd expect gcc is more clever ;). Thanks for the comparison. Anyway
if that function is performance sensitive, we can use your optimization. Just add a comment there that we want to check whether the three flags are the same in both flags and that checking your way generates better code.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR