Skip to content

Commit 0b849cb

Browse files
brianpanefolkertdev
authored andcommitted
Make State::prev_length u16
1 parent eb25760 commit 0b849cb

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

zlib-rs/src/deflate.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ pub fn init(stream: &mut z_stream, config: DeflateConfig) -> ReturnCode {
378378
_padding_1: 0,
379379
_padding_2: 0,
380380
_padding_3: [0; 6],
381+
_padding_4: [0; 6],
381382
};
382383

383384
unsafe { state_allocation.as_ptr().write(state) }; // FIXME: write is stable for NonNull since 1.80.0
@@ -684,6 +685,7 @@ pub fn copy<'a>(
684685
_padding_1: source_state._padding_1,
685686
_padding_2: source_state._padding_2,
686687
_padding_3: source_state._padding_3,
688+
_padding_4: source_state._padding_4,
687689
};
688690

689691
// write the cloned state into state_ptr
@@ -1264,7 +1266,9 @@ pub(crate) struct State<'a> {
12641266

12651267
/// Length of the best match at previous step. Matches not greater than this
12661268
/// are discarded. This is used in the lazy match evaluation.
1267-
pub(crate) prev_length: usize,
1269+
pub(crate) prev_length: u16,
1270+
1271+
_padding_4: [u8; 6],
12681272

12691273
/// To speed up deflation, hash chains are never searched beyond this length.
12701274
/// A higher limit improves compression ratio but degrades the speed.

zlib-rs/src/deflate/algorithm/slow.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn deflate_slow(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSta
5454
dist = state.strstart as isize - hash_head as isize;
5555

5656
if valid_distance_range.contains(&dist)
57-
&& state.prev_length < state.max_lazy_match as usize
57+
&& state.prev_length < state.max_lazy_match
5858
&& hash_head != 0
5959
{
6060
// To simplify the code, we prevent matches with the string
@@ -76,15 +76,15 @@ pub fn deflate_slow(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSta
7676

7777
// If there was a match at the previous step and the current
7878
// match is not better, output the previous match:
79-
if state.prev_length >= STD_MIN_MATCH && match_len <= state.prev_length {
79+
if state.prev_length as usize >= STD_MIN_MATCH && match_len <= state.prev_length as usize {
8080
let max_insert = state.strstart + state.lookahead - STD_MIN_MATCH;
8181
/* Do not insert strings in hash table beyond this. */
8282

8383
// check_match(s, state.strstart-1, state.prev_match, state.prev_length);
8484

8585
bflush = state.tally_dist(
8686
state.strstart - 1 - state.prev_match as usize,
87-
state.prev_length - STD_MIN_MATCH,
87+
state.prev_length as usize - STD_MIN_MATCH,
8888
);
8989

9090
/* Insert in hash table all strings up to the end of the match.
@@ -93,9 +93,9 @@ pub fn deflate_slow(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSta
9393
* the hash table.
9494
*/
9595
state.prev_length -= 1;
96-
state.lookahead -= state.prev_length;
96+
state.lookahead -= state.prev_length as usize;
9797

98-
let mov_fwd = state.prev_length - 1;
98+
let mov_fwd = state.prev_length as usize - 1;
9999
if max_insert > state.strstart {
100100
let insert_cnt = Ord::min(mov_fwd, max_insert - state.strstart);
101101
state.insert_string(state.strstart + 1, insert_cnt);
@@ -118,7 +118,7 @@ pub fn deflate_slow(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSta
118118
flush_block_only(stream, false);
119119
}
120120

121-
stream.state.prev_length = match_len;
121+
stream.state.prev_length = match_len as u16;
122122
stream.state.strstart += 1;
123123
stream.state.lookahead -= 1;
124124
if stream.avail_out == 0 {
@@ -127,7 +127,7 @@ pub fn deflate_slow(stream: &mut DeflateStream, flush: DeflateFlush) -> BlockSta
127127
} else {
128128
// There is no previous match to compare with, wait for
129129
// the next step to decide.
130-
state.prev_length = match_len;
130+
state.prev_length = match_len as u16;
131131
state.match_available = true;
132132
match_available = true;
133133
state.strstart += 1;

zlib-rs/src/deflate/longest_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn longest_match_help<const SLOW: bool>(
5252

5353
// length of the previous match (if any), hence <= STD_MAX_MATCH
5454
best_len = if state.prev_length > 0 {
55-
state.prev_length
55+
state.prev_length as usize
5656
} else {
5757
STD_MIN_MATCH - 1
5858
};

0 commit comments

Comments
 (0)