Skip to content

Commit 84b0fd2

Browse files
brianpanefolkertdev
authored andcommitted
Compute w_bits rather than storing it in the State structure
1 parent 221cdd7 commit 84b0fd2

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

zlib-rs/src/deflate.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ pub fn init(stream: &mut z_stream, config: DeflateConfig) -> ReturnCode {
315315
status: Status::Init,
316316

317317
// window
318-
w_bits: window_bits,
319318
w_size,
320319
w_mask: w_size - 1,
321320

@@ -376,6 +375,7 @@ pub fn init(stream: &mut z_stream, config: DeflateConfig) -> ReturnCode {
376375
_cache_line_2: (),
377376
_cache_line_3: (),
378377
_padding_0: 0,
378+
_padding_1: 0,
379379
};
380380

381381
unsafe { state_allocation.as_ptr().write(state) }; // FIXME: write is stable for NonNull since 1.80.0
@@ -665,7 +665,6 @@ pub fn copy<'a>(
665665
static_len: source_state.static_len,
666666
insert: source_state.insert,
667667
w_size: source_state.w_size,
668-
w_bits: source_state.w_bits,
669668
w_mask: source_state.w_mask,
670669
lookahead: source_state.lookahead,
671670
prev,
@@ -680,6 +679,7 @@ pub fn copy<'a>(
680679
_cache_line_2: (),
681680
_cache_line_3: (),
682681
_padding_0: source_state._padding_0,
682+
_padding_1: source_state._padding_1,
683683
};
684684

685685
// write the cloned state into state_ptr
@@ -1320,7 +1320,7 @@ pub(crate) struct State<'a> {
13201320
pub(crate) insert: usize,
13211321

13221322
pub(crate) w_size: usize, /* LZ77 window size (32K by default) */
1323-
pub(crate) w_bits: usize, /* log2(w_size) (8..16) */
1323+
_padding_1: usize,
13241324

13251325
pub(crate) w_mask: usize, /* w_size - 1 */
13261326
pub(crate) lookahead: usize, /* number of valid bytes ahead in window */
@@ -1387,6 +1387,11 @@ enum DataType {
13871387
impl<'a> State<'a> {
13881388
pub const BIT_BUF_SIZE: u8 = BitWriter::BIT_BUF_SIZE;
13891389

1390+
// log2(w_size) (in the range MIN_WBITS..=MAX_WBITS)
1391+
pub(crate) fn w_bits(&self) -> u32 {
1392+
self.w_size.trailing_zeros()
1393+
}
1394+
13901395
pub(crate) fn max_dist(&self) -> usize {
13911396
self.w_size - MIN_LOOKAHEAD
13921397
}
@@ -1543,7 +1548,7 @@ impl<'a> State<'a> {
15431548
};
15441549

15451550
let h =
1546-
(Z_DEFLATED + ((self.w_bits as u16 - 8) << 4)) << 8 | (self.level_flags() << 6) | dict;
1551+
(Z_DEFLATED + ((self.w_bits() as u16 - 8) << 4)) << 8 | (self.level_flags() << 6) | dict;
15471552

15481553
h + 31 - (h % 31)
15491554
}
@@ -3187,7 +3192,7 @@ pub fn bound(stream: Option<&mut DeflateStream>, source_len: usize) -> usize {
31873192
}
31883193
};
31893194

3190-
if stream.state.w_bits != MAX_WBITS as usize || HASH_BITS < 15 {
3195+
if stream.state.w_bits() != MAX_WBITS as u32 || HASH_BITS < 15 {
31913196
if stream.state.level == 0 {
31923197
/* upper bound for stored blocks with length 127 (memLevel == 1) ~4% overhead plus a small constant */
31933198
source_len
@@ -3420,7 +3425,7 @@ mod test {
34203425
};
34213426
assert_eq!(init(&mut stream, config), ReturnCode::Ok);
34223427
let stream = unsafe { DeflateStream::from_stream_mut(&mut stream) }.unwrap();
3423-
assert_eq!(stream.state.w_bits, 9);
3428+
assert_eq!(stream.state.w_bits(), 9);
34243429

34253430
assert!(end(stream).is_ok());
34263431
}

0 commit comments

Comments
 (0)