From 2c8703510b62bc726e1d2ff9e04e10f221b43cb2 Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Fri, 28 Nov 2025 19:31:10 -0800 Subject: Add quick_insert_value for optimized hash insertion Reduces the number of reads by two Co-authored-by: Brian Pane trifectatechfoundation/zlib-rs#374 trifectatechfoundation/zlib-rs#375 --- insert_string_tpl.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'insert_string_tpl.h') diff --git a/insert_string_tpl.h b/insert_string_tpl.h index eac1bfe045..64cc743458 100644 --- a/insert_string_tpl.h +++ b/insert_string_tpl.h @@ -51,6 +51,28 @@ Z_INTERNAL uint32_t UPDATE_HASH(uint32_t h, uint32_t val) { return h & HASH_CALC_MASK; } +/* =========================================================================== + * Quick insert string str in the dictionary using a pre-read value and set match_head + * to the previous head of the hash chain (the most recent string with same hash key). + * Return the previous length of the hash chain. + */ +Z_INTERNAL Pos QUICK_INSERT_VALUE(deflate_state *const s, uint32_t str, uint32_t val) { + uint32_t hm; + Pos head; + + HASH_CALC_VAR_INIT; + HASH_CALC(HASH_CALC_VAR, val); + HASH_CALC_VAR &= HASH_CALC_MASK; + hm = HASH_CALC_VAR; + + head = s->head[hm]; + if (LIKELY(head != str)) { + s->prev[str & s->w_mask] = head; + s->head[hm] = (Pos)str; + } + return head; +} + /* =========================================================================== * Quick insert string str in the dictionary and set match_head to the previous head * of the hash chain (the most recent string with same hash key). Return -- cgit 0.0.5-2-1-g0f52