* [PATCH] md/raid5: eliminate if-statements in cmp_stripe() @ 2023-09-03 9:50 Kuan-Wei Chiu 2023-09-03 13:30 ` Roman Mamedov 0 siblings, 1 reply; 4+ messages in thread From: Kuan-Wei Chiu @ 2023-09-03 9:50 UTC (permalink / raw) To: song; +Cc: linux-raid, linux-kernel, Kuan-Wei Chiu 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. Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com> --- drivers/md/raid5.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c index 4cb9c608ee19..b14d7ba38f0f 100644 --- a/drivers/md/raid5.c +++ b/drivers/md/raid5.c @@ -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, -- 2.25.1 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] md/raid5: eliminate if-statements in cmp_stripe() 2023-09-03 9:50 [PATCH] md/raid5: eliminate if-statements in cmp_stripe() Kuan-Wei Chiu @ 2023-09-03 13:30 ` Roman Mamedov 2023-09-03 20:10 ` Kuan-Wei Chiu 0 siblings, 1 reply; 4+ messages in thread From: Roman Mamedov @ 2023-09-03 13:30 UTC (permalink / raw) To: Kuan-Wei Chiu; +Cc: song, linux-raid, linux-kernel On Sun, 3 Sep 2023 17:50:59 +0800 Kuan-Wei Chiu <visitorckw@gmail.com> 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. > 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 <visitorckw@gmail.com> > --- > drivers/md/raid5.c | 6 +----- > 1 file changed, 1 insertion(+), 5 deletions(-) > > diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c > index 4cb9c608ee19..b14d7ba38f0f 100644 > --- a/drivers/md/raid5.c > +++ b/drivers/md/raid5.c > @@ -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, -- With respect, Roman ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] md/raid5: eliminate if-statements in cmp_stripe() 2023-09-03 13:30 ` Roman Mamedov @ 2023-09-03 20:10 ` Kuan-Wei Chiu 2023-09-05 20:49 ` Song Liu 0 siblings, 1 reply; 4+ messages in thread From: Kuan-Wei Chiu @ 2023-09-03 20:10 UTC (permalink / raw) To: Roman Mamedov; +Cc: song, linux-raid, linux-kernel On Sun, Sep 03, 2023 at 06:30:58PM +0500, Roman Mamedov wrote: > On Sun, 3 Sep 2023 17:50:59 +0800 > Kuan-Wei Chiu <visitorckw@gmail.com> 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. > 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. > > 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 <visitorckw@gmail.com> > > --- > > drivers/md/raid5.c | 6 +----- > > 1 file changed, 1 insertion(+), 5 deletions(-) > > > > diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c > > index 4cb9c608ee19..b14d7ba38f0f 100644 > > --- a/drivers/md/raid5.c > > +++ b/drivers/md/raid5.c > > @@ -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, > > > -- > With respect, > Roman -- Best regards, Kuan-Wei Chiu ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] md/raid5: eliminate if-statements in cmp_stripe() 2023-09-03 20:10 ` Kuan-Wei Chiu @ 2023-09-05 20:49 ` Song Liu 0 siblings, 0 replies; 4+ messages in thread From: Song Liu @ 2023-09-05 20:49 UTC (permalink / raw) To: Kuan-Wei Chiu; +Cc: Roman Mamedov, linux-raid, linux-kernel On Sun, Sep 3, 2023 at 1:10 PM Kuan-Wei Chiu <visitorckw@gmail.com> wrote: > > On Sun, Sep 03, 2023 at 06:30:58PM +0500, Roman Mamedov wrote: > > On Sun, 3 Sep 2023 17:50:59 +0800 > > Kuan-Wei Chiu <visitorckw@gmail.com> 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. > > > 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 [...] ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-09-05 20:59 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2023-09-03 9:50 [PATCH] md/raid5: eliminate if-statements in cmp_stripe() Kuan-Wei Chiu 2023-09-03 13:30 ` Roman Mamedov 2023-09-03 20:10 ` Kuan-Wei Chiu 2023-09-05 20:49 ` Song Liu
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox