Skip to content

Commit 09b4eb9

Browse files
brianpanefolkertdev
authored andcommitted
Make the matches field smaller to improve cache locality
1 parent 1627fc4 commit 09b4eb9

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

zlib-rs/src/deflate.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ pub fn init(stream: &mut z_stream, config: DeflateConfig) -> ReturnCode {
374374
_cache_line_1: (),
375375
_cache_line_2: (),
376376
_cache_line_3: (),
377+
_padding_0: 0,
377378
};
378379

379380
unsafe { state_allocation.as_ptr().write(state) }; // FIXME: write is stable for NonNull since 1.80.0
@@ -676,6 +677,7 @@ pub fn copy<'a>(
676677
_cache_line_1: (),
677678
_cache_line_2: (),
678679
_cache_line_3: (),
680+
_padding_0: source_state._padding_0,
679681
};
680682

681683
// write the cloned state into state_ptr
@@ -1281,6 +1283,11 @@ pub(crate) struct State<'a> {
12811283
/// than this value. This mechanism is used only for compression levels >= 4.
12821284
pub(crate) max_lazy_match: u16,
12831285

1286+
/// number of string matches in current block
1287+
/// Note: this counter is just 8 bits to help keep the struct compact. Code that
1288+
/// increments it must be careful to avoid overflow.
1289+
pub(crate) matches: u8,
1290+
12841291
/// Window position at the beginning of the current output block. Gets
12851292
/// negative when the window is moved backwards.
12861293
pub(crate) block_start: isize,
@@ -1315,9 +1322,6 @@ pub(crate) struct State<'a> {
13151322

13161323
_cache_line_2: (),
13171324

1318-
/// number of string matches in current block
1319-
pub(crate) matches: usize,
1320-
13211325
/// bit length of current block with optimal trees
13221326
opt_len: usize,
13231327
/// bit length of current block with static trees
@@ -1334,6 +1338,8 @@ pub(crate) struct State<'a> {
13341338
gzhead: Option<&'a mut gz_header>,
13351339
gzindex: usize,
13361340

1341+
_padding_0: usize,
1342+
13371343
_cache_line_3: (),
13381344

13391345
crc_fold: crate::crc32::Crc32Fold,
@@ -1457,7 +1463,7 @@ impl<'a> State<'a> {
14571463
pub(crate) fn tally_dist(&mut self, mut dist: usize, len: usize) -> bool {
14581464
self.sym_buf.push_dist(dist as u16, len as u8);
14591465

1460-
self.matches += 1;
1466+
self.matches = self.matches.saturating_add(1);
14611467
dist -= 1;
14621468

14631469
assert!(

0 commit comments

Comments
 (0)