From 44a4a5e9e29614bc168d129e68109335fa225143 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Mon, 18 Mar 2024 11:21:17 -0400 Subject: [PATCH] Use macro for wrapping += and -= The current auto-generated += and -= implementation is hard to read, and should be replaced with += where possible. That said, we cannot auto-replace it just yet because Rust behaves differently in debug mode, therefore we should use second best approach - a macro that clearly shows intention without all the boilerplate code. The only manual code are two macros in the src/enc/mod.rs Use this replacement file as described in other recent PRs to replace boilerplate code.
replacement file content ```diff @@ expression cond, expr; @@ if cond { - let _rhs = 1; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, 1); } @@ expression expr; @@ -{ - let _rhs = 1; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, 1); -} @@ expression expr; @@ -{ - let _rhs = 1u32; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs); +::wrapping_add!(expr, 1); -} @@ expression expr; @@ -{ - let _rhs = 1i32; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, 1); -} @@ expression expr; @@ -{ - let _rhs = 1; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as usize); +::wrapping_add!(expr, 1); -} @@ expression inc, expr; @@ -{ - let _rhs = inc; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, inc as u32); -} @@ expression inc, expr; @@ -{ - let _rhs = inc; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs); +::wrapping_add!(expr, inc); -} @@ expression inc, expr; @@ -{ - let _rhs = inc; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); +::wrapping_add!(expr, inc as u32); -} @@ expression inc, expr; @@ -{ - let _rhs = inc; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_add(_rhs as usize); +::wrapping_add!(expr, inc as usize); -} @@ expression expr; @@ -{ - let _rhs = 1; - let _lhs = &mut expr; - *_lhs = (*_lhs).wrapping_sub(_rhs as usize); +::wrapping_sub!(expr, 1); -} ```
--- src/enc/block_splitter.rs | 6 +----- src/enc/cluster.rs | 9 ++++----- src/enc/encode.rs | 12 ++---------- src/enc/metablock.rs | 9 ++++----- src/enc/mod.rs | 18 ++++++++++++++++++ 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/enc/block_splitter.rs b/src/enc/block_splitter.rs index 050e4476..6a6def4a 100644 --- a/src/enc/block_splitter.rs +++ b/src/enc/block_splitter.rs @@ -444,11 +444,7 @@ fn ClusterBlocks< i = 0usize; while i < length { { - { - let _rhs = 1; - let _lhs = &mut block_lengths.slice_mut()[block_idx]; - *_lhs = (*_lhs).wrapping_add(_rhs as u32); - } + ::wrapping_add!(block_lengths.slice_mut()[block_idx], 1); if i.wrapping_add(1) == length || block_ids[i] as i32 != block_ids[i.wrapping_add(1)] as i32 { diff --git a/src/enc/cluster.rs b/src/enc/cluster.rs index bb86acdf..c6b0a9ba 100644 --- a/src/enc/cluster.rs +++ b/src/enc/cluster.rs @@ -169,11 +169,10 @@ pub fn BrotliHistogramCombine< let best_idx2: u32 = (pairs[0]).idx2; HistogramSelfAddHistogram(out, (best_idx1 as usize), (best_idx2 as usize)); (out[(best_idx1 as usize)]).set_bit_cost((pairs[0]).cost_combo); - { - let _rhs = cluster_size[(best_idx2 as usize)]; - let _lhs = &mut cluster_size[(best_idx1 as usize)]; - *_lhs = (*_lhs).wrapping_add(_rhs); - } + ::wrapping_add!( + cluster_size[(best_idx1 as usize)], + cluster_size[(best_idx2 as usize)] + ); for i in 0usize..symbols_size { if symbols[i] == best_idx2 { symbols[i] = best_idx1; diff --git a/src/enc/encode.rs b/src/enc/encode.rs index fa83a83a..e85da50c 100644 --- a/src/enc/encode.rs +++ b/src/enc/encode.rs @@ -1728,16 +1728,8 @@ fn ChooseContextMap( i = 0usize; while i < 9usize { { - { - let _rhs = bigram_histo[i]; - let _lhs = &mut monogram_histo[i.wrapping_rem(3)]; - *_lhs = (*_lhs).wrapping_add(_rhs); - } - { - let _rhs = bigram_histo[i]; - let _lhs = &mut two_prefix_histo[i.wrapping_rem(6)]; - *_lhs = (*_lhs).wrapping_add(_rhs); - } + ::wrapping_add!(monogram_histo[i.wrapping_rem(3)], bigram_histo[i]); + ::wrapping_add!(two_prefix_histo[i.wrapping_rem(6)], bigram_histo[i]); } i = i.wrapping_add(1); } diff --git a/src/enc/metablock.rs b/src/enc/metablock.rs index 2c7484a1..b9ab4ddf 100644 --- a/src/enc/metablock.rs +++ b/src/enc/metablock.rs @@ -627,11 +627,10 @@ fn BlockSplitterFinishBlock< xself.merge_last_count_ = 0usize; xself.target_block_size_ = xself.min_block_size_; } else { - { - let _rhs = xself.block_size_ as u32; - let _lhs = &mut split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)]; - *_lhs = (*_lhs).wrapping_add(_rhs); - } + ::wrapping_add!( + split.lengths.slice_mut()[xself.num_blocks_.wrapping_sub(1)], + xself.block_size_ as u32 + ); histograms[xself.last_histogram_ix_[0]] = combined_histo[0].clone(); xself.last_entropy_[0] = combined_entropy[0]; if split.num_types == 1 { diff --git a/src/enc/mod.rs b/src/enc/mod.rs index 7593cfe8..6af0acfa 100644 --- a/src/enc/mod.rs +++ b/src/enc/mod.rs @@ -346,3 +346,21 @@ where read_err?; Ok(total_out.unwrap()) } + +#[macro_export] +macro_rules! wrapping_add { + ($expr:expr, $increment:expr) => {{ + let _rhs = $increment; + let _lhs = &mut $expr; + *_lhs = (*_lhs).wrapping_add(_rhs); + }}; +} + +#[macro_export] +macro_rules! wrapping_sub { + ($expr:expr, $increment:expr) => {{ + let _rhs = $increment; + let _lhs = &mut $expr; + *_lhs = (*_lhs).wrapping_sub(_rhs); + }}; +}