Skip to content

Commit d205112

Browse files
committed
Cache locality improvement for deflate State.lookahead
1 parent 322e8e1 commit d205112

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

zlib-rs/src/deflate.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ pub fn init(stream: &mut z_stream, config: DeflateConfig) -> ReturnCode {
316316

317317
// window
318318
w_size,
319-
w_mask: w_size - 1,
320319

321320
// allocated values
322321
window,
@@ -374,7 +373,7 @@ pub fn init(stream: &mut z_stream, config: DeflateConfig) -> ReturnCode {
374373
_cache_line_1: (),
375374
_cache_line_2: (),
376375
_cache_line_3: (),
377-
_padding_0: 0,
376+
_padding_0: [0; 16],
378377
};
379378

380379
unsafe { state_allocation.as_ptr().write(state) }; // FIXME: write is stable for NonNull since 1.80.0
@@ -667,7 +666,6 @@ pub fn copy<'a>(
667666
static_len: source_state.static_len,
668667
insert: source_state.insert,
669668
w_size: source_state.w_size,
670-
w_mask: source_state.w_mask,
671669
lookahead: source_state.lookahead,
672670
prev,
673671
head,
@@ -1253,7 +1251,8 @@ pub(crate) struct State<'a> {
12531251

12541252
pub(crate) window: Window<'a>,
12551253
pub(crate) w_size: usize, /* LZ77 window size (32K by default) */
1256-
pub(crate) w_mask: usize, /* w_size - 1 */
1254+
1255+
pub(crate) lookahead: usize, /* number of valid bytes ahead in window */
12571256

12581257
_cache_line_0: (),
12591258

@@ -1334,15 +1333,13 @@ pub(crate) struct State<'a> {
13341333
/// bytes at end of window left to insert
13351334
pub(crate) insert: usize,
13361335

1337-
pub(crate) lookahead: usize, /* number of valid bytes ahead in window */
1338-
13391336
/// hash index of string to be inserted
13401337
pub(crate) ins_h: u32,
13411338

13421339
gzhead: Option<&'a mut gz_header>,
13431340
gzindex: usize,
13441341

1345-
_padding_0: usize,
1342+
_padding_0: [u8; 16],
13461343

13471344
_cache_line_3: (),
13481345

@@ -1394,6 +1391,10 @@ impl<'a> State<'a> {
13941391
self.w_size.trailing_zeros()
13951392
}
13961393

1394+
pub(crate) fn w_mask(&self) -> usize {
1395+
self.w_size - 1
1396+
}
1397+
13971398
pub(crate) fn max_dist(&self) -> usize {
13981399
self.w_size - MIN_LOOKAHEAD
13991400
}

zlib-rs/src/deflate/hash_calc.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl StandardHashCalc {
4242

4343
let head = state.head.as_slice()[hm];
4444
if head != string as u16 {
45-
state.prev.as_mut_slice()[string & state.w_mask] = head;
45+
state.prev.as_mut_slice()[string & state.w_mask()] = head;
4646
state.head.as_mut_slice()[hm] = string as u16;
4747
}
4848

@@ -56,6 +56,7 @@ impl StandardHashCalc {
5656
// .take(count) generates worse assembly
5757
let slice = &slice[..Ord::min(slice.len(), count + 3)];
5858

59+
let w_mask = state.w_mask();
5960
for (i, w) in slice.windows(4).enumerate() {
6061
let idx = string as u16 + i as u16;
6162

@@ -65,7 +66,7 @@ impl StandardHashCalc {
6566

6667
let head = state.head.as_slice()[hm];
6768
if head != idx {
68-
state.prev.as_mut_slice()[idx as usize & state.w_mask] = head;
69+
state.prev.as_mut_slice()[idx as usize & w_mask] = head;
6970
state.head.as_mut_slice()[hm] = idx;
7071
}
7172
}
@@ -98,7 +99,7 @@ impl RollHashCalc {
9899

99100
let head = state.head.as_slice()[hm];
100101
if head != string as u16 {
101-
state.prev.as_mut_slice()[string & state.w_mask] = head;
102+
state.prev.as_mut_slice()[string & state.w_mask()] = head;
102103
state.head.as_mut_slice()[hm] = string as u16;
103104
}
104105

@@ -108,6 +109,7 @@ impl RollHashCalc {
108109
pub fn insert_string(state: &mut State, string: usize, count: usize) {
109110
let slice = &state.window.filled()[string + Self::HASH_CALC_OFFSET..][..count];
110111

112+
let w_mask = state.w_mask();
111113
for (i, val) in slice.iter().copied().enumerate() {
112114
let idx = string as u16 + i as u16;
113115

@@ -117,7 +119,7 @@ impl RollHashCalc {
117119

118120
let head = state.head.as_slice()[hm];
119121
if head != idx {
120-
state.prev.as_mut_slice()[idx as usize & state.w_mask] = head;
122+
state.prev.as_mut_slice()[idx as usize & w_mask] = head;
121123
state.head.as_mut_slice()[hm] = idx;
122124
}
123125
}

zlib-rs/src/deflate/longest_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn longest_match_help<const SLOW: bool>(
1919
let mut match_start = state.match_start;
2020

2121
let strstart = state.strstart;
22-
let wmask = state.w_mask;
22+
let wmask = state.w_mask();
2323
let window = state.window.filled();
2424
let scan = &window[strstart..];
2525
let mut limit: Pos;

0 commit comments

Comments
 (0)