Skip to content

Commit

Permalink
Clean up some nested curly braces
Browse files Browse the repository at this point in the history
I used a very powerful tool [coccinelle](https://gitlab.inria.fr/coccinelle/coccinelleforrust) (its C version is used for Linux kernel refactorings).  Highly recommend installing it as it has a potential to simplify refactorings while not dealing with regex errors.

To install it, run

```sh
cargo install --git https://gitlab.inria.fr/coccinelle/coccinelleforrust
```

Afterward, create this file as `target/repl.cocci` with the following content. Using `target/` ensures that it will be ignored by git.

```diff
@@
@@

 {
-    {
    ...
-    }
 }
```

and run this command from the root of the repo:

```shell
cfr --apply -c target/repl.cocci src/
cargo fmt --all
```

Afterwards, I manually reverted two macro_rules! changes -- those are the only ones that should continue having nested curlies.
  • Loading branch information
nyurik authored and danielrh committed Mar 18, 2024
1 parent b9a4857 commit b42042b
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 201 deletions.
4 changes: 1 addition & 3 deletions src/enc/backward_references/mod.rs
Expand Up @@ -2463,9 +2463,7 @@ fn CreateBackwardReferences<AH: AnyHasher>(
} < 4i32
&& (position.wrapping_add(hasher.HashTypeLength()) < pos_end)
{
{
break 'continue7;
}
break 'continue7;
}
}
break 'break6;
Expand Down
8 changes: 2 additions & 6 deletions src/enc/bit_cost.rs
Expand Up @@ -266,9 +266,7 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
s[count as usize] = i;
count += 1;
if count > 4i32 {
{
break 'break1;
}
break 'break1;
}
}
}
Expand Down Expand Up @@ -346,9 +344,7 @@ pub fn BrotliPopulationCost<HistogramType: SliceWrapper<u32> + CostAccessors>(
}
i += reps as usize;
if i == data_size {
{
break;
}
break;
}
if reps < 3 {
depth_histo[0] += reps
Expand Down
138 changes: 68 additions & 70 deletions src/enc/block_splitter.rs
Expand Up @@ -342,82 +342,80 @@ where
*item = 0;
}
for (byte_ix, data_byte_ix) in data[..length].iter().enumerate() {
{
let block_id_ptr = &mut block_id[byte_ix];
let ix: usize = byte_ix.wrapping_mul(bitmaplen);
let insert_cost_ix: usize =
u64::from(data_byte_ix.clone()).wrapping_mul(num_histograms as u64) as usize;
let mut min_cost: super::util::floatX = 1e38 as super::util::floatX;
let mut block_switch_cost: super::util::floatX = block_switch_bitcost;
if false {
// nonvectorized version: same code below
for (k, insert_cost_iter) in insert_cost
[insert_cost_ix..(insert_cost_ix + num_histograms)]
.iter()
.enumerate()
{
let cost_iter = &mut cost[(k >> 3)][k & 7];
*cost_iter += *insert_cost_iter;
if *cost_iter < min_cost {
min_cost = *cost_iter;
*block_id_ptr = k as u8;
}
}
} else {
// main (vectorized) loop
let insert_cost_slice = insert_cost.split_at(insert_cost_ix).1;
for (v_index, cost_iter) in cost
.split_at_mut(num_histograms >> 3)
.0
.iter_mut()
.enumerate()
{
let base_index = v_index << 3;
let mut local_insert_cost = [0.0 as super::util::floatX; 8];
local_insert_cost
.clone_from_slice(insert_cost_slice.split_at(base_index).1.split_at(8).0);
for sub_index in 0usize..8usize {
cost_iter[sub_index] += local_insert_cost[sub_index];
let final_cost = cost_iter[sub_index];
if final_cost < min_cost {
min_cost = final_cost;
*block_id_ptr = (base_index + sub_index) as u8;
}
}
let block_id_ptr = &mut block_id[byte_ix];
let ix: usize = byte_ix.wrapping_mul(bitmaplen);
let insert_cost_ix: usize =
u64::from(data_byte_ix.clone()).wrapping_mul(num_histograms as u64) as usize;
let mut min_cost: super::util::floatX = 1e38 as super::util::floatX;
let mut block_switch_cost: super::util::floatX = block_switch_bitcost;
if false {
// nonvectorized version: same code below
for (k, insert_cost_iter) in insert_cost
[insert_cost_ix..(insert_cost_ix + num_histograms)]
.iter()
.enumerate()
{
let cost_iter = &mut cost[(k >> 3)][k & 7];
*cost_iter += *insert_cost_iter;
if *cost_iter < min_cost {
min_cost = *cost_iter;
*block_id_ptr = k as u8;
}
let vectorized_offset = ((num_histograms >> 3) << 3);
let mut k = vectorized_offset;
//remainder loop for
for insert_cost_iter in insert_cost
.split_at(insert_cost_ix + vectorized_offset)
.1
.split_at(num_histograms & 7)
.0
.iter()
{
let cost_iter = &mut cost[(k >> 3)];
cost_iter[k & 7] += *insert_cost_iter;
if cost_iter[k & 7] < min_cost {
min_cost = cost_iter[k & 7];
*block_id_ptr = k as u8;
}
} else {
// main (vectorized) loop
let insert_cost_slice = insert_cost.split_at(insert_cost_ix).1;
for (v_index, cost_iter) in cost
.split_at_mut(num_histograms >> 3)
.0
.iter_mut()
.enumerate()
{
let base_index = v_index << 3;
let mut local_insert_cost = [0.0 as super::util::floatX; 8];
local_insert_cost
.clone_from_slice(insert_cost_slice.split_at(base_index).1.split_at(8).0);
for sub_index in 0usize..8usize {
cost_iter[sub_index] += local_insert_cost[sub_index];
let final_cost = cost_iter[sub_index];
if final_cost < min_cost {
min_cost = final_cost;
*block_id_ptr = (base_index + sub_index) as u8;
}
k += 1;
}
}
if byte_ix < 2000usize {
block_switch_cost *= (0.77 as super::util::floatX
+ 0.07 as super::util::floatX * byte_ix as (super::util::floatX)
/ 2000i32 as (super::util::floatX));
let vectorized_offset = ((num_histograms >> 3) << 3);
let mut k = vectorized_offset;
//remainder loop for
for insert_cost_iter in insert_cost
.split_at(insert_cost_ix + vectorized_offset)
.1
.split_at(num_histograms & 7)
.0
.iter()
{
let cost_iter = &mut cost[(k >> 3)];
cost_iter[k & 7] += *insert_cost_iter;
if cost_iter[k & 7] < min_cost {
min_cost = cost_iter[k & 7];
*block_id_ptr = k as u8;
}
k += 1;
}
update_cost_and_signal(
num_histograms as u32,
ix,
min_cost,
block_switch_cost,
cost,
switch_signal,
);
}
if byte_ix < 2000usize {
block_switch_cost *= (0.77 as super::util::floatX
+ 0.07 as super::util::floatX * byte_ix as (super::util::floatX)
/ 2000i32 as (super::util::floatX));
}
update_cost_and_signal(
num_histograms as u32,
ix,
min_cost,
block_switch_cost,
cost,
switch_signal,
);
}
{
let mut byte_ix: usize = length.wrapping_sub(1);
Expand Down
20 changes: 5 additions & 15 deletions src/enc/brotli_bit_stream.rs
Expand Up @@ -832,9 +832,7 @@ fn BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(
as i32
!= 0i32
{
{
break 'break5;
}
break 'break5;
}
}
codes_to_store = codes_to_store.wrapping_sub(1);
Expand Down Expand Up @@ -940,9 +938,7 @@ pub fn BrotliStoreHuffmanTree(
} else if num_codes == 1i32 {
num_codes = 2i32;
{
{
break 'break3;
}
break 'break3;
}
}
}
Expand Down Expand Up @@ -1117,9 +1113,7 @@ pub fn BrotliBuildAndStoreHuffmanTreeFast<AllocHT: alloc::Allocator<HuffmanTree>
k -= 1;
}
if BrotliSetDepth(2i32 * n - 1i32, tree.slice_mut(), depth, 14i32) {
{
break 'break11;
}
break 'break11;
}
}
}
Expand Down Expand Up @@ -1574,9 +1568,7 @@ fn BuildAndStoreHuffmanTree(
if count < 4usize {
s4[count] = i;
} else if count > 4usize {
{
break 'break31;
}
break 'break31;
}
count = count.wrapping_add(1);
}
Expand Down Expand Up @@ -1919,9 +1911,7 @@ fn RunLengthCodeZeros(
v[*out_size] = run_length_prefix.wrapping_add(extra_bits << 9);
*out_size = out_size.wrapping_add(1);
{
{
break;
}
break;
}
} else {
let extra_bits: u32 = (1u32 << max_prefix).wrapping_sub(1);
Expand Down
8 changes: 2 additions & 6 deletions src/enc/cluster.rs
Expand Up @@ -178,9 +178,7 @@ pub fn BrotliHistogramCombine<
cost_diff_threshold = 1e38 as super::util::floatX;
min_cluster_size = max_clusters;
{
{
continue;
}
continue;
}
}
/* Take the best pair from the top of heap. */
Expand Down Expand Up @@ -229,9 +227,7 @@ pub fn BrotliHistogramCombine<
|| (p).idx2 == best_idx2
{
/* Remove invalid pair from the queue. */
{
break 'continue12;
}
break 'continue12;
}
if HistogramPairIsLess(&pairs[0], &p) {
/* Replace the top of the queue if needed. */
Expand Down
6 changes: 2 additions & 4 deletions src/enc/compress_fragment.rs
Expand Up @@ -458,10 +458,8 @@ fn EmitCopyLenLastDistance(
}

fn HashBytesAtOffset(v: u64, offset: i32, shift: usize) -> u32 {
{
let h: u64 = (v >> (8i32 * offset) << 24).wrapping_mul(kHashMul32 as (u64));
(h >> shift) as u32
}
let h: u64 = (v >> (8i32 * offset) << 24).wrapping_mul(kHashMul32 as (u64));
(h >> shift) as u32
}

fn EmitCopyLen(
Expand Down
20 changes: 5 additions & 15 deletions src/enc/compress_fragment_two_pass.rs
Expand Up @@ -212,9 +212,7 @@ fn CreateCommands(
if next_ip > ip_limit {
goto_emit_remainder = 1i32;
{
{
break 'break3;
}
break 'break3;
}
}
next_hash = Hash(&base_ip[next_ip..], shift, min_match);
Expand All @@ -225,9 +223,7 @@ fn CreateCommands(
{
table[(hash as usize)] = ip_index.wrapping_sub(0) as i32;
{
{
break 'break3;
}
break 'break3;
}
}
candidate = table[(hash as usize)] as usize;
Expand All @@ -248,9 +244,7 @@ fn CreateCommands(
}
}
if goto_emit_remainder != 0 {
{
break;
}
break;
}
{
let base: usize = ip_index;
Expand Down Expand Up @@ -283,9 +277,7 @@ fn CreateCommands(
if ip_index >= ip_limit {
goto_emit_remainder = 1i32;
{
{
break;
}
break;
}
}
{
Expand Down Expand Up @@ -342,9 +334,7 @@ fn CreateCommands(
if ip_index >= ip_limit {
goto_emit_remainder = 1i32;
{
{
break;
}
break;
}
}
{
Expand Down

0 comments on commit b42042b

Please sign in to comment.