Skip to content

Commit 86707ce

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 dac157a commit 86707ce

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
@@ -20,8 +20,7 @@ use crate::enc::constants::{kCopyExtra, kInsExtra};
2020
use crate::enc::encode;
2121
use crate::enc::literal_cost::BrotliEstimateBitCostsForLiterals;
2222
use crate::enc::static_dict::{
23-
BrotliDictionary, BrotliFindAllStaticDictionaryMatches, FindMatchLengthWithLimit,
24-
BROTLI_UNALIGNED_LOAD32,
23+
BrotliDictionary, FindMatchLengthWithLimit, BROTLI_UNALIGNED_LOAD32,
2524
};
2625
use crate::enc::util::{floatX, FastLog2, FastLog2f64};
2726

@@ -382,13 +381,12 @@ where
382381
{
383382
let minlen = max(4, best_len.wrapping_add(1));
384383
if dictionary.is_some()
385-
&& BrotliFindAllStaticDictionaryMatches(
386-
dictionary.unwrap(),
384+
&& dictionary.unwrap().find_all_matches(
387385
&data[cur_ix_masked..],
388386
minlen,
389387
max_length,
390388
&mut dict_matches[..],
391-
) != 0
389+
)
392390
{
393391
assert!(params.use_dictionary);
394392
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
@@ -433,8 +433,7 @@ impl<T: SliceWrapperMut<u32> + SliceWrapper<u32> + BasicHashComputer> AnyHasher
433433
}
434434
}
435435
if dictionary.is_some() && self.buckets_.USE_DICTIONARY() != 0 && !is_match_found {
436-
is_match_found = SearchInStaticDictionary(
437-
dictionary.unwrap(),
436+
is_match_found = dictionary.unwrap().search_static_item(
438437
dictionary_hash,
439438
self,
440439
&data[cur_ix_masked..],
@@ -822,8 +821,7 @@ impl<Alloc: alloc::Allocator<u16> + alloc::Allocator<u32>> AnyHasher for H9<Allo
822821
}
823822
if !is_match_found && dictionary.is_some() {
824823
let (_, cur_data) = data.split_at(cur_ix_masked);
825-
is_match_found = SearchInStaticDictionary(
826-
dictionary.unwrap(),
824+
is_match_found = dictionary.unwrap().search_static_item(
827825
dictionary_hash,
828826
self,
829827
cur_data,
@@ -1748,8 +1746,7 @@ impl<
17481746

17491747
if !is_match_found && dictionary.is_some() {
17501748
let (_, cur_data) = data.split_at(cur_ix_masked);
1751-
is_match_found = SearchInStaticDictionary(
1752-
dictionary.unwrap(),
1749+
is_match_found = dictionary.unwrap().search_static_item(
17531750
dictionary_hash,
17541751
self,
17551752
cur_data,
@@ -1855,98 +1852,91 @@ fn Hash14(data: &[u8]) -> u32 {
18551852
h >> (32i32 - 14i32)
18561853
}
18571854

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

1904-
fn SearchInStaticDictionary<HasherType: AnyHasher>(
1905-
dictionary: &BrotliDictionary,
1906-
dictionary_hash: &[u16],
1907-
handle: &mut HasherType,
1908-
data: &[u8],
1909-
max_length: usize,
1910-
max_backward: usize,
1911-
max_distance: usize,
1912-
out: &mut HasherSearchResult,
1913-
shallow: bool,
1914-
) -> bool {
1915-
let mut key: usize;
1916-
let mut i: usize;
1917-
let mut is_match_found = false;
1918-
let opts = handle.Opts();
1919-
let xself: &mut Struct1 = handle.GetHasherCommon();
1920-
if xself.dict_num_matches < xself.dict_num_lookups >> 7 {
1921-
return false;
1922-
}
1923-
key = (Hash14(data) << 1) as usize; //FIXME: works for any kind of hasher??
1924-
i = 0usize;
1925-
while i < if shallow { 1 } else { 2 } {
1926-
{
1927-
let item: usize = dictionary_hash[key] as usize;
1899+
fn search_static_item<HasherType: AnyHasher>(
1900+
&self,
1901+
dictionary_hash: &[u16],
1902+
handle: &mut HasherType,
1903+
data: &[u8],
1904+
max_length: usize,
1905+
max_backward: usize,
1906+
max_distance: usize,
1907+
out: &mut HasherSearchResult,
1908+
shallow: bool,
1909+
) -> bool {
1910+
let mut is_match_found = false;
1911+
let opts = handle.Opts();
1912+
let xself = handle.GetHasherCommon();
1913+
if xself.dict_num_matches < xself.dict_num_lookups >> 7 {
1914+
return false;
1915+
}
1916+
let mut key = (Hash14(data) << 1) as usize; //FIXME: works for any kind of hasher??
1917+
let iterations = if shallow { 1 } else { 2 };
1918+
for _ in 0..iterations {
1919+
let item = dictionary_hash[key] as usize;
19281920
xself.dict_num_lookups = xself.dict_num_lookups.wrapping_add(1);
1929-
if item != 0usize {
1930-
let item_matches: i32 = TestStaticDictionaryItem(
1931-
dictionary,
1921+
if item != 0 {
1922+
if self.test_static_item(
19321923
item,
19331924
data,
19341925
max_length,
19351926
max_backward,
19361927
max_distance,
19371928
opts,
19381929
out,
1939-
);
1940-
if item_matches != 0 {
1930+
) {
19411931
xself.dict_num_matches = xself.dict_num_matches.wrapping_add(1);
19421932
is_match_found = true;
19431933
}
19441934
}
1935+
key += 1;
19451936
}
1946-
i = i.wrapping_add(1);
1947-
key = key.wrapping_add(1);
1937+
1938+
is_match_found
19481939
}
1949-
is_match_found
19501940
}
19511941

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

0 commit comments

Comments
 (0)