md/raid5: eliminate if-statements in cmp_stripe()
PATCH 1/1 md/raid5: eliminate if-statements in cmp_stripe()
Replace the conditional statements in the cmp_stripe() function with a branchless version to improve code readability and potentially enhance performance. The new code calculates the result using a subtraction of comparison results, making it more concise and avoiding conditional branches. This change does not alter the functionality of the code.
--- drivers/md/raid5.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/md/raid5.c +1 -5
@@ -1035,11 +1035,7 @@ static int cmp_stripe(void *priv, const struct list_head *a, struct r5pending_data, sibling);const struct r5pending_data *db =list_entry (b, struct r5pending_data, sibling); -if (da->sector > db->sector) -return 1 ; -if (da->sector < db->sector) -return -1 ; -return 0 ; +return (da->sector > db->sector) - (da->sector < db->sector); } static void dispatch_defer_bios(struct r5conf *conf, int target,
Signature · 1 line
2.25.1
Quoted 3 lines · On Sun, 3 Sep 2023 17:50:59 +0800 Kuan-Wei Chiu <[email protected]> wrote:
Replace the conditional statements in the
cmp_stripe()function with a branchless version to improve code readability and potentially enhance performance.
The new code will always do two comparisons and a subtraction (3 instructions in total), whereas the old version could return after just 1 comparison, or after 2 comparisons. So depending on the data values it is 3x to 1.5x as much operations performed than before, there unlikely to be any enhancement of performance.
Also IMO the previous version is more easily readable.
Quoted 26 lines
The new code calculates the result using a subtraction of comparison results, making it more concise and avoiding conditional branches. This change does not alter the functionality of the code.
Signed-off-by Kuan-Wei Chiu <[email protected]>--- drivers/md/raid5.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)drivers/md/raid5.c +1
-5@@ -1035,11 +1035,7 @@ static int cmp_stripe(void *priv, const struct list_head *a, struct r5pending_data, sibling);const struct r5pending_data *db =list_entry (b, struct r5pending_data, sibling); -if (da->sector > db->sector) -return 1 ; -if (da->sector < db->sector) -return -1 ; -return 0 ; +return (da->sector > db->sector) - (da->sector < db->sector); } static void dispatch_defer_bios(struct r5conf *conf, int target,
Signature · 2 lines
With respect, Roman
Quoted 7 lines · Roman Mamedov
Quoted 3 lines · On Sun, 3 Sep 2023 17:50:59 +0800 Kuan-Wei Chiu <[email protected]> wrote:
Replace the conditional statements in the
cmp_stripe()function with a branchless version to improve code readability and potentially enhance performance.
Quoted 8 lines
The new code will always do two comparisons and a subtraction (3 instructions in total), whereas the old version could return after just 1 comparison, or after 2 comparisons. So depending on the data values it is 3x to 1.5x as much operations performed than before, there unlikely to be any enhancement of performance.
Also IMO the previous version is more easily readable.
The reason behind my proposed changes was to eliminate conditional branches in the code. While the original code could occasionally achieve early returns, many compilers, such as x86-64 gcc 13.2 compiling with -O2 flag, still generate branch instructions. Processors typically have deep pipelines, and a branch prediction miss can result in a high penalty. Therefore, even though early return might not be possible, the new branchless version of code could still offer efficiency improvements.
Quoted 25 lines
Quoted 25 lines
The new code calculates the result using a subtraction of comparison results, making it more concise and avoiding conditional branches. This change does not alter the functionality of the code.
Signed-off-by Kuan-Wei Chiu <[email protected]>--- drivers/md/raid5.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)drivers/md/raid5.c +1
-5@@ -1035,11 +1035,7 @@ static int cmp_stripe(void *priv, const struct list_head *a, struct r5pending_data, sibling);const struct r5pending_data *db =list_entry (b, struct r5pending_data, sibling); -if (da->sector > db->sector) -return 1 ; -if (da->sector < db->sector) -return -1 ; -return 0 ; +return (da->sector > db->sector) - (da->sector < db->sector); }
Quoted 6 lines
Quoted 1 line
drivers/md/raid5.c +0
-0static void dispatch_defer_bios(struct r5conf *conf, int target,Signature · 2 lines
With respect, Roman
--
Best regards,
Kuan-Wei Chiu
Song Liu
maintainerQuoted 17 lines · Kuan-Wei Chiu
Quoted 7 lines · Roman Mamedov
Quoted 3 lines · On Sun, 3 Sep 2023 17:50:59 +0800 Kuan-Wei Chiu <[email protected]> wrote:
Replace the conditional statements in the
cmp_stripe()function with a branchless version to improve code readability and potentially enhance performance.Quoted 8 lines
The new code will always do two comparisons and a subtraction (3 instructions in total), whereas the old version could return after just 1 comparison, or after 2 comparisons. So depending on the data values it is 3x to 1.5x as much operations performed than before, there unlikely to be any enhancement of performance.
Also IMO the previous version is more easily readable.
Quoted 8 lines
The reason behind my proposed changes was to eliminate conditional branches in the code. While the original code could occasionally achieve early returns, many compilers, such as x86-64 gcc 13.2 compiling with -O2 flag, still generate branch instructions. Processors typically have deep pipelines, and a branch prediction miss can result in a high penalty. Therefore, even though early return might not be possible, the new branchless version of code could still offer efficiency improvements.
We need more information to support the efficiency improvement here. In this case, I would like to see some benchmark results (micro benchmark is fine).
If we cannot show the difference in performance, I would rather keep current code.
Thanks,
Song
[...]