8
8
/// Getting the parameters correct means that the resulting diff between the deflate stream
9
9
/// and the predicted deflate stream will be as small as possible.
10
10
use crate :: {
11
- hash_algorithm:: {
12
- HashAlgorithm , LibdeflateRotatingHash4 , MiniZHash , RotatingHashTrait , ZlibNGHash ,
13
- ZlibRotatingHash , MINIZ_LEVEL1_HASH_SIZE_MASK ,
14
- } ,
15
- hash_chain:: { DictionaryAddPolicy , HashChain , MAX_UPDATE_HASH_BATCH } ,
11
+ hash_algorithm:: HashAlgorithm ,
12
+ hash_chain:: DictionaryAddPolicy ,
13
+ hash_chain_holder:: { new_hash_chain_holder, HashChainHolderTrait } ,
16
14
preflate_constants,
17
15
preflate_input:: PreflateInput ,
16
+ preflate_parameter_estimator:: PreflateStrategy ,
18
17
preflate_parse_config:: { FAST_PREFLATE_PARSER_SETTINGS , SLOW_PREFLATE_PARSER_SETTINGS } ,
19
18
preflate_token:: { BlockType , PreflateToken , PreflateTokenBlock , PreflateTokenReference } ,
20
19
skip_length_estimator:: estimate_skip_length,
20
+ token_predictor:: TokenPredictorParameters ,
21
21
} ;
22
22
23
23
#[ derive( Default ) ]
@@ -30,73 +30,17 @@ pub struct CompLevelInfo {
30
30
pub max_dist_3_matches : u16 ,
31
31
pub min_len : u32 ,
32
32
pub add_policy : DictionaryAddPolicy ,
33
- pub hash_mask : u16 ,
34
- pub hash_shift : u32 ,
35
33
pub hash_algorithm : HashAlgorithm ,
36
34
pub good_length : u32 ,
37
35
pub max_lazy : u32 ,
38
36
pub nice_length : u32 ,
39
37
pub max_chain : u32 ,
40
38
}
41
39
42
- /// vtable for invoking the hash chain functions on specific implementation
43
- /// of hash algorithm
44
- trait HashChainInvoke {
45
- fn invoke_update_hash (
46
- & mut self ,
47
- len : u32 ,
48
- input : & PreflateInput ,
49
- add_policy : DictionaryAddPolicy ,
50
- ) ;
51
-
52
- fn invoke_match_depth (
53
- & mut self ,
54
- token : PreflateTokenReference ,
55
- window_size : u32 ,
56
- input : & PreflateInput ,
57
- ) -> u32 ;
58
- }
59
-
60
- /// holds the hashchain for a specific hash algorithm
61
- struct HashChainHolder < H : RotatingHashTrait > {
62
- hash_chain : HashChain < H > ,
63
- }
64
-
65
- impl < H : RotatingHashTrait + ' static > HashChainHolder < H > {
66
- fn new ( hash_shift : u32 , hash_mask : u16 , input : & PreflateInput < ' _ > ) -> Box < dyn HashChainInvoke > {
67
- Box :: new ( HashChainHolder :: < H > {
68
- hash_chain : HashChain :: < H > :: new ( hash_shift, hash_mask, input) ,
69
- } )
70
- }
71
- }
72
-
73
- impl < H : RotatingHashTrait > HashChainInvoke for HashChainHolder < H > {
74
- fn invoke_update_hash (
75
- & mut self ,
76
- len : u32 ,
77
- input : & PreflateInput ,
78
- add_policy : DictionaryAddPolicy ,
79
- ) {
80
- self . hash_chain
81
- . update_hash_with_policy :: < true > ( len, input, add_policy)
82
- }
83
-
84
- fn invoke_match_depth (
85
- & mut self ,
86
- token : PreflateTokenReference ,
87
- window_size : u32 ,
88
- input : & PreflateInput ,
89
- ) -> u32 {
90
- self . hash_chain . match_depth ( & token, window_size, input)
91
- }
92
- }
93
-
94
40
struct CandidateInfo {
95
41
hash_algorithm : HashAlgorithm ,
96
- hash_mask : u16 ,
97
- hash_shift : u32 ,
98
42
add_policy : DictionaryAddPolicy ,
99
- hash_chain : Box < dyn HashChainInvoke > ,
43
+ hash_chain : Box < dyn HashChainHolderTrait > ,
100
44
101
45
longest_dist_at_hop_0 : u32 ,
102
46
longest_dist_at_hop_1_plus : u32 ,
@@ -105,31 +49,31 @@ struct CandidateInfo {
105
49
106
50
impl CandidateInfo {
107
51
fn new (
108
- hash_mask : u16 ,
109
- hash_shift : u32 ,
110
52
add_policy : DictionaryAddPolicy ,
111
53
hash_algorithm : HashAlgorithm ,
112
- input : & PreflateInput ,
54
+ window_bits : u32 ,
113
55
) -> Self {
114
- CandidateInfo {
115
- hash_mask,
116
- hash_shift,
56
+ let params = TokenPredictorParameters {
57
+ hash_algorithm,
58
+ add_policy,
59
+ matches_to_start_detected : false ,
60
+ very_far_matches_detected : false ,
61
+ window_bits,
62
+ strategy : PreflateStrategy :: Default ,
63
+ nice_length : 0 ,
64
+ max_token_count : 0 ,
65
+ zlib_compatible : false ,
66
+ max_dist_3_matches : 0 ,
67
+ good_length : 0 ,
68
+ max_lazy : 0 ,
69
+ max_chain : 0 ,
70
+ min_len : 0 ,
71
+ } ;
72
+
73
+ Self {
117
74
add_policy,
118
75
hash_algorithm,
119
- hash_chain : match hash_algorithm {
120
- HashAlgorithm :: Zlib => {
121
- HashChainHolder :: < ZlibRotatingHash > :: new ( hash_shift, hash_mask, input)
122
- }
123
- HashAlgorithm :: MiniZFast => {
124
- HashChainHolder :: < MiniZHash > :: new ( hash_shift, hash_mask, input)
125
- }
126
- HashAlgorithm :: Libdeflate4 => {
127
- HashChainHolder :: < LibdeflateRotatingHash4 > :: new ( hash_shift, hash_mask, input)
128
- }
129
- HashAlgorithm :: ZlibNG => {
130
- HashChainHolder :: < ZlibNGHash > :: new ( hash_shift, hash_mask, input)
131
- }
132
- } ,
76
+ hash_chain : new_hash_chain_holder ( & params) ,
133
77
longest_dist_at_hop_0 : 0 ,
134
78
longest_dist_at_hop_1_plus : 0 ,
135
79
max_chain_found : 0 ,
@@ -142,9 +86,7 @@ impl CandidateInfo {
142
86
window_size : u32 ,
143
87
input : & PreflateInput ,
144
88
) -> bool {
145
- let mdepth = self
146
- . hash_chain
147
- . invoke_match_depth ( token, window_size, input) ;
89
+ let mdepth = self . hash_chain . match_depth ( token, window_size, input) ;
148
90
149
91
// remove element if the match was impossible due to matching the
150
92
// the hash depth or because in fast mode we can't match partial words
@@ -187,14 +129,6 @@ impl CandidateInfo {
187
129
self . max_chain_found
188
130
}
189
131
190
- fn hash_mask ( & self ) -> u16 {
191
- self . hash_mask
192
- }
193
-
194
- fn hash_shift ( & self ) -> u32 {
195
- self . hash_shift
196
- }
197
-
198
132
fn hash_algorithm ( & self ) -> HashAlgorithm {
199
133
self . hash_algorithm
200
134
}
@@ -243,39 +177,34 @@ impl<'a> CompLevelEstimatorState<'a> {
243
177
let mut candidates: Vec < Box < CandidateInfo > > = Vec :: new ( ) ;
244
178
245
179
candidates. push ( Box :: new ( CandidateInfo :: new (
246
- MINIZ_LEVEL1_HASH_SIZE_MASK ,
247
- 0 ,
248
180
add_policy,
249
181
HashAlgorithm :: MiniZFast ,
250
- & input ,
182
+ wbits ,
251
183
) ) ) ;
252
184
253
185
for ( hash_shift, hash_mask) in [ ( 5 , 32767 ) , ( 4 , 2047 ) ] {
254
186
candidates. push ( Box :: new ( CandidateInfo :: new (
255
- hash_mask,
256
- hash_shift,
257
187
add_policy,
258
- HashAlgorithm :: Zlib ,
259
- & input,
188
+ HashAlgorithm :: Zlib {
189
+ hash_mask,
190
+ hash_shift,
191
+ } ,
192
+ wbits,
260
193
) ) ) ;
261
194
}
262
195
263
196
// LibFlate4 candidate
264
197
candidates. push ( Box :: new ( CandidateInfo :: new (
265
- 0xffff ,
266
- 0 ,
267
198
add_policy,
268
199
HashAlgorithm :: Libdeflate4 ,
269
- & input ,
200
+ wbits ,
270
201
) ) ) ;
271
202
272
203
// ZlibNG candidate
273
204
candidates. push ( Box :: new ( CandidateInfo :: new (
274
- 0xffff ,
275
- 0 ,
276
205
add_policy,
277
206
HashAlgorithm :: ZlibNG ,
278
- & input ,
207
+ wbits ,
279
208
) ) ) ;
280
209
281
210
CompLevelEstimatorState {
@@ -291,25 +220,14 @@ impl<'a> CompLevelEstimatorState<'a> {
291
220
}
292
221
}
293
222
294
- fn update_hash ( & mut self , mut length : u32 , override_add_policy : bool ) {
295
- while length > 0 {
296
- let batch_len = std:: cmp:: min ( length, MAX_UPDATE_HASH_BATCH ) ;
297
-
298
- for i in & mut self . candidates {
299
- i. hash_chain . invoke_update_hash (
300
- batch_len,
301
- & self . input ,
302
- if override_add_policy {
303
- DictionaryAddPolicy :: AddAll
304
- } else {
305
- i. add_policy
306
- } ,
307
- ) ;
308
- }
309
-
310
- self . input . advance ( batch_len) ;
311
- length -= batch_len;
223
+ fn update_hash ( & mut self , length : u32 , override_add_policy : bool ) {
224
+ for i in & mut self . candidates {
225
+ let mut inputc = self . input . clone ( ) ;
226
+ i. hash_chain
227
+ . update_hash_with_depth ( length, & mut inputc, override_add_policy) ;
312
228
}
229
+
230
+ self . input . advance ( length) ;
313
231
}
314
232
315
233
fn check_match ( & mut self , token : PreflateTokenReference ) {
@@ -371,8 +289,6 @@ impl<'a> CompLevelEstimatorState<'a> {
371
289
let mut max_lazy = 258 ;
372
290
let mut nice_length = 258 ;
373
291
374
- let hash_mask = candidate. hash_mask ( ) ;
375
- let hash_shift = candidate. hash_shift ( ) ;
376
292
let add_policy = candidate. add_policy ;
377
293
let max_chain = candidate. max_chain_found ( ) + 1 ;
378
294
let hash_algorithm = candidate. hash_algorithm ( ) ;
@@ -419,8 +335,6 @@ impl<'a> CompLevelEstimatorState<'a> {
419
335
matches_to_start_detected : self . match_to_start ,
420
336
very_far_matches_detected : very_far_matches,
421
337
max_dist_3_matches : self . longest_len_3_dist as u16 ,
422
- hash_mask,
423
- hash_shift,
424
338
add_policy,
425
339
good_length,
426
340
max_lazy,
0 commit comments