Skip to content

Commit

Permalink
Inline hash_mul_.
Browse files Browse the repository at this point in the history
`hash_mul_` is a constant, but is currently passed into `FN(HashBytes)` as a parameter.

Provides a 0.3-08% speedup for the `H6` hasher.

PiperOrigin-RevId: 616503395
  • Loading branch information
Brotli authored and Copybara-Service committed Mar 17, 2024
1 parent 9717649 commit 286cca0
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions c/enc/hash_longest_match64_inc.h
Expand Up @@ -20,9 +20,8 @@ static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }

/* HashBytes is the function that chooses the bucket to place the address in. */
static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* BROTLI_RESTRICT data,
uint64_t hash_mul) {
const uint64_t h = BROTLI_UNALIGNED_LOAD64LE(data) * hash_mul;
static BROTLI_INLINE size_t FN(HashBytes)(const uint8_t* BROTLI_RESTRICT data) {
const uint64_t h = BROTLI_UNALIGNED_LOAD64LE(data) * (kHashMul64 << (64 - 5 * 8));
/* The higher bits contain more mixture from the multiplication,
so we take our results from there. */
return (size_t)(h >> (64 - 15));
Expand All @@ -34,8 +33,6 @@ typedef struct HashLongestMatch {
/* Only block_size_ newest backward references are kept,
and the older are forgotten. */
size_t block_size_;
/* Hash multiplier tuned to match length. */
uint64_t hash_mul_;
/* Mask for accessing entries in a block (in a ring-buffer manner). */
uint32_t block_mask_;

Expand All @@ -60,7 +57,6 @@ static void FN(Initialize)(
self->common_ = common;

BROTLI_UNUSED(params);
self->hash_mul_ = kHashMul64 << (64 - 5 * 8);
BROTLI_DCHECK(common->params.bucket_bits == 15);
self->bucket_size_ = (size_t)1 << common->params.bucket_bits;
self->block_bits_ = common->params.block_bits;
Expand All @@ -81,7 +77,7 @@ static void FN(Prepare)(
if (one_shot && input_size <= partial_prepare_threshold) {
size_t i;
for (i = 0; i < input_size; ++i) {
const size_t key = FN(HashBytes)(&data[i], self->hash_mul_);
const size_t key = FN(HashBytes)(&data[i]);
num[key] = 0;
}
} else {
Expand All @@ -107,7 +103,7 @@ static BROTLI_INLINE void FN(Store)(
const size_t mask, const size_t ix) {
uint16_t* BROTLI_RESTRICT num = self->num_;
uint32_t* BROTLI_RESTRICT buckets = self->buckets_;
const size_t key = FN(HashBytes)(&data[ix & mask], self->hash_mul_);
const size_t key = FN(HashBytes)(&data[ix & mask]);
const size_t minor_ix = num[key] & self->block_mask_;
const size_t offset = minor_ix + (key << self->block_bits_);
++num[key];
Expand Down Expand Up @@ -212,7 +208,7 @@ static BROTLI_INLINE void FN(FindLongestMatch)(
}
}
{
const size_t key = FN(HashBytes)(&data[cur_ix_masked], self->hash_mul_);
const size_t key = FN(HashBytes)(&data[cur_ix_masked]);
uint32_t* BROTLI_RESTRICT bucket = &buckets[key << self->block_bits_];
const size_t down =
(num[key] > self->block_size_) ?
Expand Down

0 comments on commit 286cca0

Please sign in to comment.