diff mbox series

tcp_bbr: replace lambda expression with bitwise operation for bit flip

Message ID 20240426152011.37069-1-richard120310@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series tcp_bbr: replace lambda expression with bitwise operation for bit flip | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 926 this patch: 926
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 937 this patch: 937
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 937 this patch: 937
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest fail net-next-2024-04-26--18-00 (tests: 993)

Commit Message

I Hsin Cheng April 26, 2024, 3:20 p.m. UTC
In the origin implementation in function bbr_update_ack_aggregation(),
we utilize a lambda expression to flip the bit value of
bbr->extra_acked_win_idx. Since the data type of
bbr->extra_acked_win_idx is simply a single bit, we are actually trying
to perform a bit flip operation, under the fact we can simply perform a
bitwise not operation on bbr->extra_acked_win_idx.

This way we can elimate the need of possible branches which generate by
the lambda function, they could result in branch misses sometimes.
Perform a bitwise not operation is more straightforward and wouldn't
generate branches.

Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
---
 net/ipv4/tcp_bbr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Eric Dumazet April 26, 2024, 3:32 p.m. UTC | #1
On Fri, Apr 26, 2024 at 5:20 PM I Hsin Cheng <richard120310@gmail.com> wrote:
>
> In the origin implementation in function bbr_update_ack_aggregation(),
> we utilize a lambda expression to flip the bit value of
> bbr->extra_acked_win_idx. Since the data type of
> bbr->extra_acked_win_idx is simply a single bit, we are actually trying
> to perform a bit flip operation, under the fact we can simply perform a
> bitwise not operation on bbr->extra_acked_win_idx.
>
> This way we can elimate the need of possible branches which generate by
> the lambda function, they could result in branch misses sometimes.
> Perform a bitwise not operation is more straightforward and wouldn't
> generate branches.
>
> Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
> ---
>  net/ipv4/tcp_bbr.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/net/ipv4/tcp_bbr.c b/net/ipv4/tcp_bbr.c
> index 146792cd2..75068ba25 100644
> --- a/net/ipv4/tcp_bbr.c
> +++ b/net/ipv4/tcp_bbr.c
> @@ -829,8 +829,7 @@ static void bbr_update_ack_aggregation(struct sock *sk,
>                                                 bbr->extra_acked_win_rtts + 1);
>                 if (bbr->extra_acked_win_rtts >= bbr_extra_acked_win_rtts) {
>                         bbr->extra_acked_win_rtts = 0;
> -                       bbr->extra_acked_win_idx = bbr->extra_acked_win_idx ?
> -                                                  0 : 1;
> +                       bbr->extra_acked_win_idx = ~(bbr->extra_acked_win_idx);
>                         bbr->extra_acked[bbr->extra_acked_win_idx] = 0;
>                 }
>         }

Or

bbr->extra_acked_win_idx ^= 1;

Note that C compilers generate the same code, for the 3 variants.

They do not generate branches for something simple like this.
I Hsin Cheng April 26, 2024, 5:01 p.m. UTC | #2
On Fri, Apr 26, 2024 at 05:32:57PM +0200, Eric Dumazet wrote:
> On Fri, Apr 26, 2024 at 5:20 PM I Hsin Cheng <richard120310@gmail.com> wrote:
> >
> > In the origin implementation in function bbr_update_ack_aggregation(),
> > we utilize a lambda expression to flip the bit value of
> > bbr->extra_acked_win_idx. Since the data type of
> > bbr->extra_acked_win_idx is simply a single bit, we are actually trying
> > to perform a bit flip operation, under the fact we can simply perform a
> > bitwise not operation on bbr->extra_acked_win_idx.
> >
> > This way we can elimate the need of possible branches which generate by
> > the lambda function, they could result in branch misses sometimes.
> > Perform a bitwise not operation is more straightforward and wouldn't
> > generate branches.
> >
> > Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
> > ---
> >  net/ipv4/tcp_bbr.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/net/ipv4/tcp_bbr.c b/net/ipv4/tcp_bbr.c
> > index 146792cd2..75068ba25 100644
> > --- a/net/ipv4/tcp_bbr.c
> > +++ b/net/ipv4/tcp_bbr.c
> > @@ -829,8 +829,7 @@ static void bbr_update_ack_aggregation(struct sock *sk,
> >                                                 bbr->extra_acked_win_rtts + 1);
> >                 if (bbr->extra_acked_win_rtts >= bbr_extra_acked_win_rtts) {
> >                         bbr->extra_acked_win_rtts = 0;
> > -                       bbr->extra_acked_win_idx = bbr->extra_acked_win_idx ?
> > -                                                  0 : 1;
> > +                       bbr->extra_acked_win_idx = ~(bbr->extra_acked_win_idx);
> >                         bbr->extra_acked[bbr->extra_acked_win_idx] = 0;
> >                 }
> >         }
> 
> Or
> 
> bbr->extra_acked_win_idx ^= 1;
> 
> Note that C compilers generate the same code, for the 3 variants.
> 
> They do not generate branches for something simple like this.

I see, thanks for your explanation.
I thought the compilers behavior might alters due to different 
architecture or different compilers.
So would you recommend on the proposed changes or we should stick to
 the original implementation? 
Personally I think my version or your proposed change are both more 
understandable and elegant than the lambda expression.
Al Viro April 26, 2024, 8:19 p.m. UTC | #3
On Sat, Apr 27, 2024 at 01:01:21AM +0800, I Hsin Cheng wrote:

> I see, thanks for your explanation.
> I thought the compilers behavior might alters due to different 
> architecture or different compilers.
> So would you recommend on the proposed changes or we should stick to
>  the original implementation? 
> Personally I think my version or your proposed change are both more 
> understandable and elegant than the lambda expression.

Out of curiosity, where do you see any lambda expressions in the entire
thing?
I Hsin Cheng April 27, 2024, 8:31 a.m. UTC | #4
On Fri, Apr 26, 2024 at 09:19:02PM +0100, Al Viro wrote:
> On Sat, Apr 27, 2024 at 01:01:21AM +0800, I Hsin Cheng wrote:
> 
> > I see, thanks for your explanation.
> > I thought the compilers behavior might alters due to different 
> > architecture or different compilers.
> > So would you recommend on the proposed changes or we should stick to
> >  the original implementation? 
> > Personally I think my version or your proposed change are both more 
> > understandable and elegant than the lambda expression.
> 
> Out of curiosity, where do you see any lambda expressions in the entire
> thing?

Sorry, it's my fault to address the expression as "lambda expression",
it should be called as "conditional" or "ternary" operator.

Thanks for your remind.
diff mbox series

Patch

diff --git a/net/ipv4/tcp_bbr.c b/net/ipv4/tcp_bbr.c
index 146792cd2..75068ba25 100644
--- a/net/ipv4/tcp_bbr.c
+++ b/net/ipv4/tcp_bbr.c
@@ -829,8 +829,7 @@  static void bbr_update_ack_aggregation(struct sock *sk,
 						bbr->extra_acked_win_rtts + 1);
 		if (bbr->extra_acked_win_rtts >= bbr_extra_acked_win_rtts) {
 			bbr->extra_acked_win_rtts = 0;
-			bbr->extra_acked_win_idx = bbr->extra_acked_win_idx ?
-						   0 : 1;
+			bbr->extra_acked_win_idx = ~(bbr->extra_acked_win_idx);
 			bbr->extra_acked[bbr->extra_acked_win_idx] = 0;
 		}
 	}