tcp_bbr: replace lambda expression with bitwise operation for bit flip
Subsystems (4)
PATCH 1/1 tcp_bbr: replace lambda expression with bitwise operation for bit flip
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.
--- net/ipv4/tcp_bbr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/ipv4/tcp_bbr.c +1 -2
@@ -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 ; } }
Signature · 1 line
2.34.1
Quoted 23 lines · I Hsin Cheng
In the origin implementation in function
bbr_update_ack_aggregation(), we utilize a lambda expression to flip the bit value ofbbr->extra_acked_win_idx. Since the data type ofbbr->extra_acked_win_idxis 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 onbbr->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 <[email protected]>--- net/ipv4/tcp_bbr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)net/ipv4/tcp_bbr.c +0
-0
net/ipv4/tcp_bbr.c static void bbr_update_ack_aggregation(struct sock *sk, · line 835
Quoted 9 lines
net/ipv4/tcp_bbr.c +1
-2@@ -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.
Quoted 36 lines · Eric Dumazet
Quoted 23 lines · I Hsin Cheng
In the origin implementation in function
bbr_update_ack_aggregation(), we utilize a lambda expression to flip the bit value ofbbr->extra_acked_win_idx. Since the data type ofbbr->extra_acked_win_idxis 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 onbbr->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 <[email protected]>--- net/ipv4/tcp_bbr.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)net/ipv4/tcp_bbr.c +0
-0net/ipv4/tcp_bbr.c static void bbr_update_ack_aggregation(struct sock *sk, · line 835
Quoted 9 lines
net/ipv4/tcp_bbr.c +1
-2@@ -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
Quoted 5 lines
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.
Quoted 7 lines · I Hsin Cheng
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?
Quoted 10 lines · Al Viro
Quoted 7 lines · I Hsin Cheng
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.
Quoted 2 lines
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.