From 905f6515169b8fb3f3c79fd8c44aa48ff8d7befe Mon Sep 17 00:00:00 2001 From: Brotli Date: Sat, 16 Mar 2024 11:18:51 -0700 Subject: [PATCH] Don't check `cur_ix_masked` against `ring_buffer_mask`. `cur_ix_masked` isn't changing from iteration to iteration, and `max_length` ensures we never find a match long enough to walk off the ring buffer. PiperOrigin-RevId: 616440826 --- c/enc/hash.h | 6 ++---- c/enc/hash_forgetful_chain_inc.h | 3 +-- c/enc/hash_longest_match64_inc.h | 6 ++---- c/enc/hash_longest_match_inc.h | 6 ++---- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/c/enc/hash.h b/c/enc/hash.h index 5677d8292..cf935d542 100644 --- a/c/enc/hash.h +++ b/c/enc/hash.h @@ -586,8 +586,7 @@ static BROTLI_INLINE void FindCompoundDictionaryMatch( limit = source_size - offset; limit = (limit > max_length) ? max_length : limit; if (distance > max_distance) continue; - if (cur_ix_masked + best_len > ring_buffer_mask || - best_len >= limit || + if (best_len >= limit || data[cur_ix_masked + best_len] != source[offset + best_len]) { continue; } @@ -664,8 +663,7 @@ static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches( limit = source_size - offset; limit = (limit > max_length) ? max_length : limit; if (distance > max_distance) continue; - if (cur_ix_masked + best_len > ring_buffer_mask || - best_len >= limit || + if (best_len >= limit || data[cur_ix_masked + best_len] != source[offset + best_len]) { continue; } diff --git a/c/enc/hash_forgetful_chain_inc.h b/c/enc/hash_forgetful_chain_inc.h index 48e1cdcdf..38e33072c 100644 --- a/c/enc/hash_forgetful_chain_inc.h +++ b/c/enc/hash_forgetful_chain_inc.h @@ -255,8 +255,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( prev_ix = (cur_ix - backward) & ring_buffer_mask; slot = banks[bank].slots[last].next; delta = banks[bank].slots[last].delta; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { continue; } diff --git a/c/enc/hash_longest_match64_inc.h b/c/enc/hash_longest_match64_inc.h index e48fc6155..874d90510 100644 --- a/c/enc/hash_longest_match64_inc.h +++ b/c/enc/hash_longest_match64_inc.h @@ -184,8 +184,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( } prev_ix &= ring_buffer_mask; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { continue; } @@ -228,8 +227,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( break; } prev_ix &= ring_buffer_mask; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { continue; } diff --git a/c/enc/hash_longest_match_inc.h b/c/enc/hash_longest_match_inc.h index 788e9ef99..6dbae0603 100644 --- a/c/enc/hash_longest_match_inc.h +++ b/c/enc/hash_longest_match_inc.h @@ -181,8 +181,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( } prev_ix &= ring_buffer_mask; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { continue; } @@ -221,8 +220,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)( break; } prev_ix &= ring_buffer_mask; - if (cur_ix_masked + best_len > ring_buffer_mask || - prev_ix + best_len > ring_buffer_mask || + if (prev_ix + best_len > ring_buffer_mask || data[cur_ix_masked + best_len] != data[prev_ix + best_len]) { continue; }