Skip to content

Commit

Permalink
Use proper self for Command and related
Browse files Browse the repository at this point in the history
Use `hide whitespace changes` when reviewing
  • Loading branch information
nyurik authored and danielrh committed Apr 18, 2024
1 parent 2c601f4 commit f1dad22
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 270 deletions.
2 changes: 1 addition & 1 deletion src/bin/brotli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ where
);
util::write_one(&tmp);
for cmd in data.iter() {
util::write_one(&brotli::thaw_pair(cmd, &mb));
util::write_one(&cmd.thaw_pair(&mb));
}
};
if params.log_meta_block {
Expand Down
4 changes: 2 additions & 2 deletions src/enc/backward_references/hash_to_binary_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use alloc::{Allocator, SliceWrapper, SliceWrapperMut};
use core;
use core::cmp::{max, min};
use enc::command::{
CombineLengthCodes, Command, CommandCopyLen, ComputeDistanceCode, GetCopyLengthCode,
GetInsertLengthCode, InitCommand, PrefixEncodeCopyDistance,
CombineLengthCodes, Command, ComputeDistanceCode, GetCopyLengthCode, GetInsertLengthCode,
PrefixEncodeCopyDistance,
};
use enc::constants::{kCopyExtra, kInsExtra};
use enc::dictionary_hash::kStaticDictionaryHash;
Expand Down
9 changes: 4 additions & 5 deletions src/enc/backward_references/hq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use alloc::{Allocator, SliceWrapper, SliceWrapperMut};
use core;
use core::cmp::{max, min};
use enc::command::{
BrotliDistanceParams, CombineLengthCodes, Command, CommandCopyLen, ComputeDistanceCode,
GetCopyLengthCode, GetInsertLengthCode, InitCommand, PrefixEncodeCopyDistance,
BrotliDistanceParams, CombineLengthCodes, Command, ComputeDistanceCode, GetCopyLengthCode,
GetInsertLengthCode, PrefixEncodeCopyDistance,
};
use enc::constants::{kCopyExtra, kInsExtra};
use enc::dictionary_hash::kStaticDictionaryHash;
Expand Down Expand Up @@ -137,8 +137,7 @@ pub fn BrotliZopfliCreateCommands(
let max_distance: usize = min(block_start.wrapping_add(pos), max_backward_limit);
let is_dictionary = distance > max_distance.wrapping_add(gap);
let dist_code: usize = next.distance_code() as usize;
InitCommand(
&mut commands[i],
commands[i].init(
&params.dist,
insert_length,
copy_length,
Expand Down Expand Up @@ -1146,7 +1145,7 @@ impl<AllocF: Allocator<floatX>> ZopfliCostModel<AllocF> {
while i < num_commands {
{
let inslength: usize = (commands[i]).insert_len_ as usize;
let copylength: usize = CommandCopyLen(&commands[i]) as usize;
let copylength: usize = commands[i].copy_len() as usize;
let distcode: usize = (commands[i].dist_prefix_ as i32 & 0x03ff) as usize;
let cmdcode: usize = (commands[i]).cmd_prefix_ as usize;
{
Expand Down
13 changes: 5 additions & 8 deletions src/enc/backward_references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod hq;
mod test;

use super::super::alloc::{Allocator, SliceWrapper, SliceWrapperMut};
use super::command::{BrotliDistanceParams, Command, ComputeDistanceCode, InitCommand};
use super::command::{BrotliDistanceParams, Command, ComputeDistanceCode};
use super::dictionary_hash::kStaticDictionaryHash;
use super::hash_to_binary_tree::{H10Buckets, H10DefaultParams, ZopfliNode, H10};
use super::static_dict::BrotliDictionary;
Expand Down Expand Up @@ -2472,13 +2472,10 @@ fn CreateBackwardReferences<AH: AnyHasher>(
hasher.PrepareDistanceCache(dist_cache);
}
new_commands_count += 1;
InitCommand(
{
let (mut _old, new_commands) =
core::mem::take(&mut commands).split_at_mut(1);
commands = new_commands;
&mut _old[0]
},

let (old, new_commands) = core::mem::take(&mut commands).split_at_mut(1);
commands = new_commands;
old[0].init(
&params.dist,
insert_length,
sr.len,
Expand Down
8 changes: 2 additions & 6 deletions src/enc/block_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ fn CountLiterals(cmds: &[Command], num_commands: usize) -> usize {
total_length
}

fn CommandCopyLen(xself: &Command) -> u32 {
xself.copy_len_ & 0x01ff_ffff
}

fn CopyLiteralsToByteArray(
cmds: &[Command],
num_commands: usize,
Expand Down Expand Up @@ -155,7 +151,7 @@ fn CopyLiteralsToByteArray(
}
from_pos = from_pos
.wrapping_add(insert_len)
.wrapping_add(CommandCopyLen(&cmds[i]) as usize)
.wrapping_add(cmds[i].copy_len() as usize)
& mask;
}
}
Expand Down Expand Up @@ -972,7 +968,7 @@ pub fn BrotliSplitBlock<
let mut j: usize = 0usize;
for i in 0usize..num_commands {
let cmd = &cmds[i];
if CommandCopyLen(cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
if cmd.copy_len() != 0 && cmd.cmd_prefix_ >= 128 {
distance_prefixes.slice_mut()[j] = cmd.dist_prefix_ & 0x03ff;
j = j.wrapping_add(1);
}
Expand Down
38 changes: 11 additions & 27 deletions src/enc/brotli_bit_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ use super::super::dictionary::{
kBrotliDictionary, kBrotliDictionaryOffsetsByLength, kBrotliDictionarySizeBitsByLength,
};
use super::super::transform::TransformDictionaryWord;
use super::command::{
Command, CommandDistanceIndexAndOffset, GetCopyLengthCode, GetInsertLengthCode,
};
use super::command::{Command, GetCopyLengthCode, GetInsertLengthCode};
use super::constants::{
kCodeLengthBits, kCodeLengthDepth, kCopyBase, kCopyExtra, kInsBase, kInsExtra,
kNonZeroRepsBits, kNonZeroRepsDepth, kSigned3BitContextLookup, kStaticCommandCodeBits,
Expand Down Expand Up @@ -307,10 +305,10 @@ fn process_command_queue<'a, CmdProcessor: interface::CommandProcessor<'a>>(
let (inserts, interim) = input_iter.split_at(min(cmd.insert_len_ as usize, mb_len));
recoder_state.num_bytes_encoded += inserts.len();
let _copy_cursor = input.len() - interim.len();
// let distance_context = CommandDistanceContext(cmd);
// let distance_context = cmd.distance_context();
let copylen_code = cmd.copy_len_code();

let (prev_dist_index, dist_offset) = CommandDistanceIndexAndOffset(cmd, &params.dist);
let (prev_dist_index, dist_offset) = cmd.distance_index_and_offset(&params.dist);
let final_distance: usize;
if prev_dist_index == 0 {
final_distance = dist_offset as usize;
Expand Down Expand Up @@ -2089,19 +2087,6 @@ impl<Alloc: Allocator<u8> + Allocator<u16>> BlockEncoder<'_, Alloc> {
}
}

fn CommandCopyLen(xself: &Command) -> u32 {
xself.copy_len_ & 0x01ff_ffff
}

fn CommandDistanceContext(xself: &Command) -> u32 {
let r: u32 = (xself.cmd_prefix_ as i32 >> 6) as u32;
let c: u32 = (xself.cmd_prefix_ as i32 & 7i32) as u32;
if (r == 0u32 || r == 2u32 || r == 4u32 || r == 7u32) && (c <= 2u32) {
return c;
}
3u32
}

impl<Alloc: Allocator<u8> + Allocator<u16>> BlockEncoder<'_, Alloc> {
fn cleanup(&mut self, m: &mut Alloc) {
<Alloc as Allocator<u8>>::free_cell(m, core::mem::take(&mut self.depths_));
Expand Down Expand Up @@ -2315,8 +2300,8 @@ pub fn BrotliStoreMetaBlock<Alloc: BrotliAlloc, Cb>(
j = j.wrapping_sub(1);
}
}
pos = pos.wrapping_add(CommandCopyLen(&cmd) as usize);
if CommandCopyLen(&cmd) != 0 {
pos = pos.wrapping_add(cmd.copy_len() as usize);
if cmd.copy_len() != 0 {
prev_byte2 = input[(pos.wrapping_sub(2) & mask)];
prev_byte = input[(pos.wrapping_sub(1) & mask)];
if cmd.cmd_prefix_ as i32 >= 128i32 {
Expand All @@ -2326,10 +2311,9 @@ pub fn BrotliStoreMetaBlock<Alloc: BrotliAlloc, Cb>(
if mb.distance_context_map_size == 0usize {
distance_enc.store_symbol(dist_code, storage_ix, storage);
} else {
let context: usize = CommandDistanceContext(&cmd) as usize;
distance_enc.store_symbol_with_context(
dist_code,
context,
cmd.distance_context() as usize,
mb.distance_context_map.slice(),
storage_ix,
storage,
Expand Down Expand Up @@ -2371,8 +2355,8 @@ fn BuildHistograms(
}
j = j.wrapping_sub(1);
}
pos = pos.wrapping_add(CommandCopyLen(&cmd) as usize);
if CommandCopyLen(&cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
pos = pos.wrapping_add(cmd.copy_len() as usize);
if cmd.copy_len() != 0 && cmd.cmd_prefix_ >= 128 {
HistogramAddItem(dist_histo, cmd.dist_prefix_ as usize & 0x03ff);
}
}
Expand Down Expand Up @@ -2418,8 +2402,8 @@ fn StoreDataWithHuffmanCodes(
}
j = j.wrapping_sub(1);
}
pos = pos.wrapping_add(CommandCopyLen(&cmd) as usize);
if CommandCopyLen(&cmd) != 0 && (cmd.cmd_prefix_ as i32 >= 128i32) {
pos = pos.wrapping_add(cmd.copy_len() as usize);
if cmd.copy_len() != 0 && cmd.cmd_prefix_ >= 128 {
let dist_code: usize = cmd.dist_prefix_ as usize & 0x03ff;
let distnumextra: u32 = u32::from(cmd.dist_prefix_) >> 10;
let distextra: u32 = cmd.dist_extra_;
Expand Down Expand Up @@ -2728,7 +2712,7 @@ pub fn BrotliStoreMetaBlockFast<Cb, Alloc: BrotliAlloc>(
j = j.wrapping_sub(1);
}
num_literals = num_literals.wrapping_add(cmd.insert_len_ as usize);
pos = pos.wrapping_add(CommandCopyLen(&cmd) as usize);
pos = pos.wrapping_add(cmd.copy_len() as usize);
}
BrotliBuildAndStoreHuffmanTreeFast(
m,
Expand Down

0 comments on commit f1dad22

Please sign in to comment.