Skip to content

Commit

Permalink
Use bools instead of i32s (second part)
Browse files Browse the repository at this point in the history
There are a few publicly visible functions that should also be using a `bool` type.  In order not to cause an API change, I created a crate-visible variants so that we can remove them in the next major release. In the meantime, users will see a deprecation warning IF they are using these functions (most likely they are not) - but if they are, they will let us know to keep them public.

Note a few extra `FIXME` -- these I think we should fix when making a breaking change, but not just yet because they are harder to do in a backward-compatible way
  • Loading branch information
nyurik committed May 13, 2024
1 parent dac157a commit 908062a
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 102 deletions.
6 changes: 3 additions & 3 deletions src/enc/backward_references/hq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{
BrotliEncoderParams,
};
use crate::enc::command::{
BrotliDistanceParams, CombineLengthCodes, Command, GetCopyLengthCode, GetInsertLengthCode,
combine_length_codes, BrotliDistanceParams, Command, GetCopyLengthCode, GetInsertLengthCode,
PrefixEncodeCopyDistance,
};
use crate::enc::constants::{kCopyExtra, kInsExtra};
Expand Down Expand Up @@ -737,7 +737,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
let dist_cost = base_cost + model.get_distance_cost(j);
for l in best_len.wrapping_add(1)..=len {
let copycode: u16 = GetCopyLengthCode(l);
let cmdcode: u16 = CombineLengthCodes(inscode, copycode, (j == 0usize) as i32);
let cmdcode = combine_length_codes(inscode, copycode, j == 0);
let cost: floatX = (if cmdcode < 128 { base_cost } else { dist_cost })
+ (GetCopyExtra(copycode) as floatX)
+ model.get_command_cost(cmdcode);
Expand Down Expand Up @@ -790,7 +790,7 @@ fn UpdateNodes<AllocF: Allocator<floatX>>(
len
};
let copycode: u16 = GetCopyLengthCode(len_code);
let cmdcode: u16 = CombineLengthCodes(inscode, copycode, 0i32);
let cmdcode = combine_length_codes(inscode, copycode, false);
let cost: floatX = dist_cost
+ GetCopyExtra(copycode) as (floatX)
+ model.get_command_cost(cmdcode);
Expand Down
2 changes: 2 additions & 0 deletions src/enc/backward_references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct BrotliEncoderParams {
pub lgblock: i32,
/// how big the source file is (or 0 if no hint is provided)
pub size_hint: usize,
// FIXME: this should be bool
/// avoid serializing out priors for literal sections in the favor of decode speed
pub disable_literal_context_modeling: i32,
pub hasher: BrotliHasherParams,
Expand Down Expand Up @@ -120,6 +121,7 @@ pub enum HowPrepared {
#[derive(Clone, PartialEq)]
pub struct Struct1 {
pub params: BrotliHasherParams,
/// FIXME: this should be bool
pub is_prepared_: i32,
pub dict_num_lookups: usize,
pub dict_num_matches: usize,
Expand Down
196 changes: 185 additions & 11 deletions src/enc/brotli_bit_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,22 +1338,22 @@ fn BrotliEncodeMlen(length: u32, bits: &mut u64, numbits: &mut u32, nibblesbits:
}

fn StoreCompressedMetaBlockHeader(
is_final_block: i32,
is_final_block: bool,
length: usize,
storage_ix: &mut usize,
storage: &mut [u8],
) {
let mut lenbits: u64 = 0;
let mut nlenbits: u32 = 0;
let mut nibblesbits: u32 = 0;
BrotliWriteBits(1, is_final_block as (u64), storage_ix, storage);
if is_final_block != 0 {
BrotliWriteBits(1, is_final_block.into(), storage_ix, storage);
if is_final_block {
BrotliWriteBits(1, 0, storage_ix, storage);
}
BrotliEncodeMlen(length as u32, &mut lenbits, &mut nlenbits, &mut nibblesbits);
BrotliWriteBits(2, nibblesbits as u64, storage_ix, storage);
BrotliWriteBits(nlenbits as u8, lenbits, storage_ix, storage);
if is_final_block == 0 {
if !is_final_block {
BrotliWriteBits(1, 0, storage_ix, storage);
}
}
Expand Down Expand Up @@ -2092,15 +2092,65 @@ pub fn JumpToByteBoundary(storage_ix: &mut usize, storage: &mut [u8]) {
storage[(*storage_ix >> 3)] = 0u8;
}

#[deprecated(note = "use store_meta_block instead")]
pub fn BrotliStoreMetaBlock<Alloc: BrotliAlloc, Cb>(
alloc: &mut Alloc,
input: &[u8],
start_pos: usize,
length: usize,
mask: usize,
prev_byte: u8,
prev_byte2: u8,
is_last: i32,
params: &BrotliEncoderParams,
literal_context_mode: ContextType,
distance_cache: &[i32; kNumDistanceCacheEntries],
commands: &[Command],
n_commands: usize,
mb: &mut MetaBlockSplit<Alloc>,
recoder_state: &mut RecoderState,
storage_ix: &mut usize,
storage: &mut [u8],
callback: &mut Cb,
) where
Cb: FnMut(
&mut interface::PredictionModeContextMap<InputReferenceMut>,
&mut [StaticCommand],
InputPair,
&mut Alloc,
),
{
store_meta_block(
alloc,
input,
start_pos,
length,
mask,
prev_byte,
prev_byte2,
is_last != 0,
params,
literal_context_mode,
distance_cache,
commands,
n_commands,
mb,
recoder_state,
storage_ix,
storage,
callback,
)
}

pub(crate) fn store_meta_block<Alloc: BrotliAlloc, Cb>(
alloc: &mut Alloc,
input: &[u8],
start_pos: usize,
length: usize,
mask: usize,
mut prev_byte: u8,
mut prev_byte2: u8,
is_last: i32,
is_last: bool,
params: &BrotliEncoderParams,
literal_context_mode: ContextType,
distance_cache: &[i32; kNumDistanceCacheEntries],
Expand Down Expand Up @@ -2320,7 +2370,7 @@ pub fn BrotliStoreMetaBlock<Alloc: BrotliAlloc, Cb>(
distance_enc.cleanup(alloc);
command_enc.cleanup(alloc);
literal_enc.cleanup(alloc);
if is_last != 0 {
if is_last {
JumpToByteBoundary(storage_ix, storage);
}
}
Expand Down Expand Up @@ -2412,6 +2462,8 @@ fn StoreDataWithHuffmanCodes(
}

fn nop<'a>(_data: &[interface::Command<InputReference>]) {}

#[deprecated(note = "use store_meta_block_trivial instead")]
pub fn BrotliStoreMetaBlockTrivial<Alloc: BrotliAlloc, Cb>(
alloc: &mut Alloc,
input: &[u8],
Expand All @@ -2423,6 +2475,47 @@ pub fn BrotliStoreMetaBlockTrivial<Alloc: BrotliAlloc, Cb>(
distance_cache: &[i32; kNumDistanceCacheEntries],
commands: &[Command],
n_commands: usize,
recoder_state: &mut crate::enc::brotli_bit_stream::RecoderState,
storage_ix: &mut usize,
storage: &mut [u8],
f: &mut Cb,
) where
Cb: FnMut(
&mut interface::PredictionModeContextMap<InputReferenceMut>,
&mut [StaticCommand],
InputPair,
&mut Alloc,
),
{
store_meta_block_trivial(
alloc,
input,
start_pos,
length,
mask,
is_last != 0,
params,
distance_cache,
commands,
n_commands,
recoder_state,
storage_ix,
storage,
f,
)
}

pub(crate) fn store_meta_block_trivial<Alloc: BrotliAlloc, Cb>(
alloc: &mut Alloc,
input: &[u8],
start_pos: usize,
length: usize,
mask: usize,
is_last: bool,
params: &BrotliEncoderParams,
distance_cache: &[i32; kNumDistanceCacheEntries],
commands: &[Command],
n_commands: usize,
recoder_state: &mut RecoderState,
storage_ix: &mut usize,
storage: &mut [u8],
Expand Down Expand Up @@ -2525,7 +2618,7 @@ pub fn BrotliStoreMetaBlockTrivial<Alloc: BrotliAlloc, Cb>(
storage_ix,
storage,
);
if is_last != 0 {
if is_last {
JumpToByteBoundary(storage_ix, storage);
}
}
Expand Down Expand Up @@ -2641,6 +2734,7 @@ impl RecoderState {
}
}

#[deprecated(note = "use store_meta_block_fast instead")]
pub fn BrotliStoreMetaBlockFast<Cb, Alloc: BrotliAlloc>(
m: &mut Alloc,
input: &[u8],
Expand All @@ -2659,7 +2753,48 @@ pub fn BrotliStoreMetaBlockFast<Cb, Alloc: BrotliAlloc>(
) where
Cb: FnMut(
&mut interface::PredictionModeContextMap<InputReferenceMut>,
&mut [interface::StaticCommand],
&mut [StaticCommand],
InputPair,
&mut Alloc,
),
{
store_meta_block_fast(
m,
input,
start_pos,
length,
mask,
is_last != 0,
params,
dist_cache,
commands,
n_commands,
recoder_state,
storage_ix,
storage,
cb,
);
}

pub(crate) fn store_meta_block_fast<Cb, Alloc: BrotliAlloc>(
m: &mut Alloc,
input: &[u8],
start_pos: usize,
length: usize,
mask: usize,
is_last: bool,
params: &BrotliEncoderParams,
dist_cache: &[i32; kNumDistanceCacheEntries],
commands: &[Command],
n_commands: usize,
recoder_state: &mut RecoderState,
storage_ix: &mut usize,
storage: &mut [u8],
cb: &mut Cb,
) where
Cb: FnMut(
&mut interface::PredictionModeContextMap<InputReferenceMut>,
&mut [StaticCommand],
InputPair,
&mut Alloc,
),
Expand Down Expand Up @@ -2802,7 +2937,7 @@ pub fn BrotliStoreMetaBlockFast<Cb, Alloc: BrotliAlloc>(
storage,
);
}
if is_last != 0 {
if is_last {
JumpToByteBoundary(storage_ix, storage);
}
}
Expand Down Expand Up @@ -2837,6 +2972,8 @@ fn InputPairFromMaskedInput(
}
(&input[masked_pos..masked_pos + len], &[])
}

#[deprecated(note = "use store_uncompressed_meta_block instead")]
pub fn BrotliStoreUncompressedMetaBlock<Cb, Alloc: BrotliAlloc>(
alloc: &mut Alloc,
is_final_block: i32,
Expand All @@ -2853,7 +2990,44 @@ pub fn BrotliStoreUncompressedMetaBlock<Cb, Alloc: BrotliAlloc>(
) where
Cb: FnMut(
&mut interface::PredictionModeContextMap<InputReferenceMut>,
&mut [interface::StaticCommand],
&mut [StaticCommand],
InputPair,
&mut Alloc,
),
{
store_uncompressed_meta_block(
alloc,
is_final_block != 0,
input,
position,
mask,
params,
len,
recoder_state,
storage_ix,
storage,
suppress_meta_block_logging,
cb,
)
}

pub(crate) fn store_uncompressed_meta_block<Cb, Alloc: BrotliAlloc>(
alloc: &mut Alloc,
is_final_block: bool,
input: &[u8],
position: usize,
mask: usize,
params: &BrotliEncoderParams,
len: usize,
recoder_state: &mut RecoderState,
storage_ix: &mut usize,
storage: &mut [u8],
suppress_meta_block_logging: bool,
cb: &mut Cb,
) where
Cb: FnMut(
&mut interface::PredictionModeContextMap<InputReferenceMut>,
&mut [StaticCommand],
InputPair,
&mut Alloc,
),
Expand Down Expand Up @@ -2890,7 +3064,7 @@ pub fn BrotliStoreUncompressedMetaBlock<Cb, Alloc: BrotliAlloc>(
cb,
);
}
if is_final_block != 0 {
if is_final_block {
BrotliWriteBits(1u8, 1u64, storage_ix, storage);
BrotliWriteBits(1u8, 1u64, storage_ix, storage);
JumpToByteBoundary(storage_ix, storage);
Expand Down
Loading

0 comments on commit 908062a

Please sign in to comment.