Skip to content

Commit 5e3f957

Browse files
committed
Simplify static_dict
* Convert `IsMatch(dict, ...)` into a member method. The original `IsMatch` is marked as deprecated - and we probably can delete it, but it will require a major release due to being a breaking change. So for now, lets keep it, but remove it when releasing a new breaking version * Same for `BrotliFindAllStaticDictionaryMatches` and a few more methods (they were not moved though to allow for easier review) * lots of internal simplifications - like using direct comparisons, using bools, etc
1 parent c35494b commit 5e3f957

File tree

3 files changed

+504
-568
lines changed

3 files changed

+504
-568
lines changed

src/enc/backward_references/hq.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use crate::enc::constants::{kCopyExtra, kInsExtra};
2121
use crate::enc::encode;
2222
use crate::enc::literal_cost::BrotliEstimateBitCostsForLiterals;
2323
use crate::enc::static_dict::{
24-
BrotliDictionary, BrotliFindAllStaticDictionaryMatches, FindMatchLengthWithLimit,
25-
BROTLI_UNALIGNED_LOAD32,
24+
BrotliDictionary, FindMatchLengthWithLimit, BROTLI_UNALIGNED_LOAD32,
2625
};
2726
use crate::enc::util::{floatX, FastLog2, FastLog2f64};
2827

@@ -381,13 +380,12 @@ where
381380
{
382381
let minlen = max(4, best_len.wrapping_add(1));
383382
if dictionary.is_some()
384-
&& BrotliFindAllStaticDictionaryMatches(
385-
dictionary.unwrap(),
383+
&& dictionary.unwrap().find_all_matches(
386384
&data[cur_ix_masked..],
387385
minlen,
388386
max_length,
389387
&mut dict_matches[..],
390-
) != 0
388+
)
391389
{
392390
assert!(params.use_dictionary);
393391
let maxlen = min(37, max_length);

src/enc/backward_references/mod.rs

Lines changed: 69 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
436436
}
437437
}
438438
if dictionary.is_some() && self.buckets_.USE_DICTIONARY() != 0 && !is_match_found {
439-
is_match_found = SearchInStaticDictionary(
440-
dictionary.unwrap(),
439+
is_match_found = dictionary.unwrap().search_static_item(
441440
dictionary_hash,
442441
self,
443442
&data[cur_ix_masked..],
@@ -825,8 +824,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
825824
}
826825
if !is_match_found && dictionary.is_some() {
827826
let (_, cur_data) = data.split_at(cur_ix_masked);
828-
is_match_found = SearchInStaticDictionary(
829-
dictionary.unwrap(),
827+
is_match_found = dictionary.unwrap().search_static_item(
830828
dictionary_hash,
831829
self,
832830
cur_data,
@@ -1752,8 +1750,7 @@ impl<
17521750

17531751
if !is_match_found && dictionary.is_some() {
17541752
let (_, cur_data) = data.split_at(cur_ix_masked);
1755-
is_match_found = SearchInStaticDictionary(
1756-
dictionary.unwrap(),
1753+
is_match_found = dictionary.unwrap().search_static_item(
17571754
dictionary_hash,
17581755
self,
17591756
cur_data,
@@ -1859,98 +1856,91 @@ fn Hash14(data: &[u8]) -> u32 {
18591856
h >> (32i32 - 14i32)
18601857
}
18611858

1862-
fn TestStaticDictionaryItem(
1863-
dictionary: &BrotliDictionary,
1864-
item: usize,
1865-
data: &[u8],
1866-
max_length: usize,
1867-
max_backward: usize,
1868-
max_distance: usize,
1869-
h9_opts: H9Opts,
1870-
out: &mut HasherSearchResult,
1871-
) -> i32 {
1872-
let backward: usize;
1873-
1874-
let len: usize = item & 0x1fusize;
1875-
let dist: usize = item >> 5;
1876-
let offset: usize =
1877-
(dictionary.offsets_by_length[len] as usize).wrapping_add(len.wrapping_mul(dist));
1878-
if len > max_length {
1879-
return 0i32;
1880-
}
1881-
let matchlen: usize = FindMatchLengthWithLimit(data, &dictionary.data[offset..], len);
1882-
if matchlen.wrapping_add(kCutoffTransformsCount as usize) <= len || matchlen == 0usize {
1883-
return 0i32;
1884-
}
1885-
{
1886-
let cut: u64 = len.wrapping_sub(matchlen) as u64;
1887-
let transform_id: usize =
1859+
impl BrotliDictionary {
1860+
fn test_static_item(
1861+
&self,
1862+
item: usize,
1863+
data: &[u8],
1864+
max_length: usize,
1865+
max_backward: usize,
1866+
max_distance: usize,
1867+
h9_opts: H9Opts,
1868+
out: &mut HasherSearchResult,
1869+
) -> bool {
1870+
let len = item & 0x1f;
1871+
let dist = item >> 5;
1872+
let offset = (self.offsets_by_length[len] as usize).wrapping_add(len.wrapping_mul(dist));
1873+
if len > max_length {
1874+
return false;
1875+
}
1876+
let matchlen: usize = FindMatchLengthWithLimit(data, &self.data[offset..], len);
1877+
if matchlen.wrapping_add(kCutoffTransformsCount as usize) <= len || matchlen == 0 {
1878+
return false;
1879+
}
1880+
1881+
let cut = len.wrapping_sub(matchlen) as u64;
1882+
let transform_id =
18881883
(cut << 2).wrapping_add(kCutoffTransforms >> cut.wrapping_mul(6) & 0x3f) as usize;
1889-
backward = max_backward
1884+
let backward = max_backward
18901885
.wrapping_add(dist)
18911886
.wrapping_add(1)
1892-
.wrapping_add(transform_id << dictionary.size_bits_by_length[len] as i32);
1893-
}
1894-
if backward > max_distance {
1895-
return 0i32;
1896-
}
1897-
let score: u64 = BackwardReferenceScore(matchlen, backward, h9_opts);
1898-
if score < out.score {
1899-
return 0i32;
1887+
.wrapping_add(transform_id << self.size_bits_by_length[len]);
1888+
1889+
if backward > max_distance {
1890+
return false;
1891+
}
1892+
let score = BackwardReferenceScore(matchlen, backward, h9_opts);
1893+
if score < out.score {
1894+
return false;
1895+
}
1896+
out.len = matchlen;
1897+
out.len_x_code = len ^ matchlen;
1898+
out.distance = backward;
1899+
out.score = score;
1900+
true
19001901
}
1901-
out.len = matchlen;
1902-
out.len_x_code = len ^ matchlen;
1903-
out.distance = backward;
1904-
out.score = score;
1905-
1i32
1906-
}
19071902

1908-
fn SearchInStaticDictionary<HasherType: AnyHasher>(
1909-
dictionary: &BrotliDictionary,
1910-
dictionary_hash: &[u16],
1911-
handle: &mut HasherType,
1912-
data: &[u8],
1913-
max_length: usize,
1914-
max_backward: usize,
1915-
max_distance: usize,
1916-
out: &mut HasherSearchResult,
1917-
shallow: bool,
1918-
) -> bool {
1919-
let mut key: usize;
1920-
let mut i: usize;
1921-
let mut is_match_found = false;
1922-
let opts = handle.Opts();
1923-
let xself: &mut Struct1 = handle.GetHasherCommon();
1924-
if xself.dict_num_matches < xself.dict_num_lookups >> 7 {
1925-
return false;
1926-
}
1927-
key = (Hash14(data) << 1) as usize; //FIXME: works for any kind of hasher??
1928-
i = 0usize;
1929-
while i < if shallow { 1 } else { 2 } {
1930-
{
1931-
let item: usize = dictionary_hash[key] as usize;
1903+
fn search_static_item<HasherType: AnyHasher>(
1904+
&self,
1905+
dictionary_hash: &[u16],
1906+
handle: &mut HasherType,
1907+
data: &[u8],
1908+
max_length: usize,
1909+
max_backward: usize,
1910+
max_distance: usize,
1911+
out: &mut HasherSearchResult,
1912+
shallow: bool,
1913+
) -> bool {
1914+
let mut is_match_found = false;
1915+
let opts = handle.Opts();
1916+
let xself = handle.GetHasherCommon();
1917+
if xself.dict_num_matches < xself.dict_num_lookups >> 7 {
1918+
return false;
1919+
}
1920+
let mut key = (Hash14(data) << 1) as usize; //FIXME: works for any kind of hasher??
1921+
let iterations = if shallow { 1 } else { 2 };
1922+
for _ in 0..iterations {
1923+
let item = dictionary_hash[key] as usize;
19321924
xself.dict_num_lookups = xself.dict_num_lookups.wrapping_add(1);
1933-
if item != 0usize {
1934-
let item_matches: i32 = TestStaticDictionaryItem(
1935-
dictionary,
1925+
if item != 0 {
1926+
if self.test_static_item(
19361927
item,
19371928
data,
19381929
max_length,
19391930
max_backward,
19401931
max_distance,
19411932
opts,
19421933
out,
1943-
);
1944-
if item_matches != 0 {
1934+
) {
19451935
xself.dict_num_matches = xself.dict_num_matches.wrapping_add(1);
19461936
is_match_found = true;
19471937
}
19481938
}
1939+
key += 1;
19491940
}
1950-
i = i.wrapping_add(1);
1951-
key = key.wrapping_add(1);
1941+
1942+
is_match_found
19521943
}
1953-
is_match_found
19541944
}
19551945

19561946
impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> CloneWithAlloc<Alloc>

0 commit comments

Comments
 (0)