See file LICENSE for detail or copy at https://opensource.org/licenses/MIT\r
*/\r
\r
-/* template parameters: FN, BUCKET_BITS, BLOCK_BITS,\r
- NUM_LAST_DISTANCES_TO_CHECK */\r
+/* template parameters: FN */\r
\r
/* A (forgetful) hash table to the data seen by the compressor, to\r
help create backward references to previous data.\r
\r
- This is a hash map of fixed size (BUCKET_SIZE) to a ring buffer of\r
- fixed size (BLOCK_SIZE). The ring buffer contains the last BLOCK_SIZE\r
+ This is a hash map of fixed size (bucket_size_) to a ring buffer of\r
+ fixed size (block_size_). The ring buffer contains the last block_size_\r
index positions of the given hash key in the compressed data. */\r
\r
#define HashLongestMatch HASHER()\r
\r
-/* Number of hash buckets. */\r
-#define BUCKET_SIZE (1 << BUCKET_BITS)\r
-\r
-/* Only BLOCK_SIZE newest backward references are kept,\r
- and the older are forgotten. */\r
-#define BLOCK_SIZE (1u << BLOCK_BITS)\r
-\r
-/* Mask for accessing entries in a block (in a ringbuffer manner). */\r
-#define BLOCK_MASK ((1 << BLOCK_BITS) - 1)\r
-\r
-#define HASH_MAP_SIZE (2 << BUCKET_BITS)\r
-\r
static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }\r
static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }\r
\r
-/* HashBytes is the function that chooses the bucket to place\r
- the address in. The HashLongestMatch and HashLongestMatchQuickly\r
- classes have separate, different implementations of hashing. */\r
-static uint32_t FN(HashBytes)(const uint8_t *data) {\r
- uint32_t h = BROTLI_UNALIGNED_LOAD32(data) * kHashMul32;\r
+/* HashBytes is the function that chooses the bucket to place the address in. */\r
+static uint32_t FN(HashBytes)(const uint8_t* data, const int shift) {\r
+ uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;\r
/* The higher bits contain more mixture from the multiplication,\r
so we take our results from there. */\r
- return h >> (32 - BUCKET_BITS);\r
+ return (uint32_t)(h >> shift);\r
}\r
\r
typedef struct HashLongestMatch {\r
- /* Number of entries in a particular bucket. */\r
- uint16_t num_[BUCKET_SIZE];\r
+ /* Number of hash buckets. */\r
+ size_t bucket_size_;\r
+ /* Only block_size_ newest backward references are kept,\r
+ and the older are forgotten. */\r
+ size_t block_size_;\r
+ /* Left-shift for computing hash bucket index from hash value. */\r
+ int hash_shift_;\r
+ /* Mask for accessing entries in a block (in a ring-buffer manner). */\r
+ uint32_t block_mask_;\r
\r
- /* Buckets containing BLOCK_SIZE of backward references. */\r
- uint32_t buckets_[BLOCK_SIZE << BUCKET_BITS];\r
+ /* --- Dynamic size members --- */\r
\r
- /* True if num_ array needs to be initialized. */\r
- BROTLI_BOOL is_dirty_;\r
+ /* Number of entries in a particular bucket. */\r
+ /* uint16_t num[bucket_size]; */\r
\r
- DictionarySearchStatictics dict_search_stats_;\r
+ /* Buckets containing block_size_ of backward references. */\r
+ /* uint32_t* buckets[bucket_size * block_size]; */\r
} HashLongestMatch;\r
\r
-static void FN(Reset)(HashLongestMatch* self) {\r
- self->is_dirty_ = BROTLI_TRUE;\r
- DictionarySearchStaticticsReset(&self->dict_search_stats_);\r
+static BROTLI_INLINE HashLongestMatch* FN(Self)(HasherHandle handle) {\r
+ return (HashLongestMatch*)&(GetHasherCommon(handle)[1]);\r
}\r
\r
-static void FN(InitEmpty)(HashLongestMatch* self) {\r
- if (self->is_dirty_) {\r
- memset(self->num_, 0, sizeof(self->num_));\r
- self->is_dirty_ = BROTLI_FALSE;\r
- }\r
+static BROTLI_INLINE uint16_t* FN(Num)(HashLongestMatch* self) {\r
+ return (uint16_t*)(&self[1]);\r
}\r
\r
-static void FN(InitForData)(HashLongestMatch* self, const uint8_t* data,\r
- size_t num) {\r
- size_t i;\r
- for (i = 0; i < num; ++i) {\r
- const uint32_t key = FN(HashBytes)(&data[i]);\r
- self->num_[key] = 0;\r
- }\r
- if (num != 0) {\r
- self->is_dirty_ = BROTLI_FALSE;\r
- }\r
+static BROTLI_INLINE uint32_t* FN(Buckets)(HashLongestMatch* self) {\r
+ return (uint32_t*)(&FN(Num)(self)[self->bucket_size_]);\r
}\r
\r
-static void FN(Init)(\r
- MemoryManager* m, HashLongestMatch* self, const uint8_t* data,\r
- const BrotliEncoderParams* params, size_t position, size_t bytes,\r
- BROTLI_BOOL is_last) {\r
- /* Choose which init method is faster.\r
- Init() is about 100 times faster than InitForData(). */\r
- const size_t kMaxBytesForPartialHashInit = HASH_MAP_SIZE >> 7;\r
- BROTLI_UNUSED(m);\r
+static void FN(Initialize)(\r
+ HasherHandle handle, const BrotliEncoderParams* params) {\r
+ HasherCommon* common = GetHasherCommon(handle);\r
+ HashLongestMatch* self = FN(Self)(handle);\r
BROTLI_UNUSED(params);\r
- if (position == 0 && is_last && bytes <= kMaxBytesForPartialHashInit) {\r
- FN(InitForData)(self, data, bytes);\r
+ self->hash_shift_ = 32 - common->params.bucket_bits;\r
+ self->bucket_size_ = (size_t)1 << common->params.bucket_bits;\r
+ self->block_size_ = (size_t)1 << common->params.block_bits;\r
+ self->block_mask_ = (uint32_t)(self->block_size_ - 1);\r
+}\r
+\r
+static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,\r
+ size_t input_size, const uint8_t* data) {\r
+ HashLongestMatch* self = FN(Self)(handle);\r
+ uint16_t* num = FN(Num)(self);\r
+ /* Partial preparation is 100 times slower (per socket). */\r
+ size_t partial_prepare_threshold = self->bucket_size_ >> 6;\r
+ if (one_shot && input_size <= partial_prepare_threshold) {\r
+ size_t i;\r
+ for (i = 0; i < input_size; ++i) {\r
+ const uint32_t key = FN(HashBytes)(&data[i], self->hash_shift_);\r
+ num[key] = 0;\r
+ }\r
} else {\r
- FN(InitEmpty)(self);\r
+ memset(num, 0, self->bucket_size_ * sizeof(num[0]));\r
}\r
}\r
\r
+static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(\r
+ const BrotliEncoderParams* params, BROTLI_BOOL one_shot,\r
+ size_t input_size) {\r
+ size_t bucket_size = (size_t)1 << params->hasher.bucket_bits;\r
+ size_t block_size = (size_t)1 << params->hasher.block_bits;\r
+ BROTLI_UNUSED(one_shot);\r
+ BROTLI_UNUSED(input_size);\r
+ return sizeof(HashLongestMatch) + bucket_size * (2 + 4 * block_size);\r
+}\r
+\r
/* Look at 4 bytes at &data[ix & mask].\r
Compute a hash from these, and store the value of ix at that position. */\r
-static BROTLI_INLINE void FN(Store)(HashLongestMatch* self, const uint8_t *data,\r
+static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data,\r
const size_t mask, const size_t ix) {\r
- const uint32_t key = FN(HashBytes)(&data[ix & mask]);\r
- const size_t minor_ix = self->num_[key] & BLOCK_MASK;\r
- self->buckets_[minor_ix + (key << BLOCK_BITS)] = (uint32_t)ix;\r
- ++self->num_[key];\r
+ HashLongestMatch* self = FN(Self)(handle);\r
+ uint16_t* num = FN(Num)(self);\r
+ const uint32_t key = FN(HashBytes)(&data[ix & mask], self->hash_shift_);\r
+ const size_t minor_ix = num[key] & self->block_mask_;\r
+ const size_t offset =\r
+ minor_ix + (key << GetHasherCommon(handle)->params.block_bits);\r
+ FN(Buckets)(self)[offset] = (uint32_t)ix;\r
+ ++num[key];\r
}\r
\r
-static BROTLI_INLINE void FN(StoreRange)(HashLongestMatch* self,\r
- const uint8_t *data, const size_t mask, const size_t ix_start,\r
+static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,\r
+ const uint8_t* data, const size_t mask, const size_t ix_start,\r
const size_t ix_end) {\r
size_t i;\r
for (i = ix_start; i < ix_end; ++i) {\r
- FN(Store)(self, data, mask, i);\r
+ FN(Store)(handle, data, mask, i);\r
}\r
}\r
\r
-static BROTLI_INLINE void FN(StitchToPreviousBlock)(HashLongestMatch* self,\r
+static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle,\r
size_t num_bytes, size_t position, const uint8_t* ringbuffer,\r
size_t ringbuffer_mask) {\r
if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {\r
/* Prepare the hashes for three last bytes of the last write.\r
These could not be calculated before, since they require knowledge\r
of both the previous and the current block. */\r
- FN(Store)(self, ringbuffer, ringbuffer_mask, position - 3);\r
- FN(Store)(self, ringbuffer, ringbuffer_mask, position - 2);\r
- FN(Store)(self, ringbuffer, ringbuffer_mask, position - 1);\r
+ FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 3);\r
+ FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 2);\r
+ FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 1);\r
}\r
}\r
\r
+static BROTLI_INLINE void FN(PrepareDistanceCache)(\r
+ HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {\r
+ PrepareDistanceCache(distance_cache,\r
+ GetHasherCommon(handle)->params.num_last_distances_to_check);\r
+}\r
+\r
/* Find a longest backward match of &data[cur_ix] up to the length of\r
max_length and stores the position cur_ix in the hash table.\r
\r
+ REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache\r
+ values; if this method is invoked repeatedly with the same distance\r
+ cache values, it is enough to invoke FN(PrepareDistanceCache) once.\r
+\r
Does not look for matches longer than max_length.\r
Does not look for matches further away than max_backward.\r
Writes the best match into |out|.\r
- Returns true when match is found, otherwise false. */\r
-static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(HashLongestMatch* self,\r
+ |out|->score is updated only if a better match is found. */\r
+static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle,\r
+ const BrotliEncoderDictionary* dictionary,\r
const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,\r
const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,\r
- const size_t max_length, const size_t max_backward,\r
- HasherSearchResult* BROTLI_RESTRICT out) {\r
+ const size_t max_length, const size_t max_backward, const size_t gap,\r
+ const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) {\r
+ HasherCommon* common = GetHasherCommon(handle);\r
+ HashLongestMatch* self = FN(Self)(handle);\r
+ uint16_t* num = FN(Num)(self);\r
+ uint32_t* buckets = FN(Buckets)(self);\r
const size_t cur_ix_masked = cur_ix & ring_buffer_mask;\r
- BROTLI_BOOL is_match_found = BROTLI_FALSE;\r
/* Don't accept a short copy from far away. */\r
+ score_t min_score = out->score;\r
score_t best_score = out->score;\r
size_t best_len = out->len;\r
size_t i;\r
out->len = 0;\r
- out->len_x_code = 0;\r
+ out->len_code_delta = 0;\r
/* Try last distance first. */\r
- for (i = 0; i < NUM_LAST_DISTANCES_TO_CHECK; ++i) {\r
- const size_t idx = kDistanceCacheIndex[i];\r
- const size_t backward =\r
- (size_t)(distance_cache[idx] + kDistanceCacheOffset[i]);\r
+ for (i = 0; i < (size_t)common->params.num_last_distances_to_check; ++i) {\r
+ const size_t backward = (size_t)distance_cache[i];\r
size_t prev_ix = (size_t)(cur_ix - backward);\r
if (prev_ix >= cur_ix) {\r
continue;\r
}\r
- if (PREDICT_FALSE(backward > max_backward)) {\r
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {\r
continue;\r
}\r
prev_ix &= ring_buffer_mask;\r
/* Comparing for >= 2 does not change the semantics, but just saves for\r
a few unnecessary binary logarithms in backward reference score,\r
since we are not interested in such short matches. */\r
- score_t score = BackwardReferenceScoreUsingLastDistance(len, i);\r
+ score_t score = BackwardReferenceScoreUsingLastDistance(len);\r
if (best_score < score) {\r
- best_score = score;\r
- best_len = len;\r
- out->len = best_len;\r
- out->distance = backward;\r
- out->score = best_score;\r
- is_match_found = BROTLI_TRUE;\r
+ if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);\r
+ if (best_score < score) {\r
+ best_score = score;\r
+ best_len = len;\r
+ out->len = best_len;\r
+ out->distance = backward;\r
+ out->score = best_score;\r
+ }\r
}\r
}\r
}\r
}\r
{\r
- const uint32_t key = FN(HashBytes)(&data[cur_ix_masked]);\r
- uint32_t* BROTLI_RESTRICT bucket = &self->buckets_[key << BLOCK_BITS];\r
+ const uint32_t key =\r
+ FN(HashBytes)(&data[cur_ix_masked], self->hash_shift_);\r
+ uint32_t* BROTLI_RESTRICT bucket =\r
+ &buckets[key << common->params.block_bits];\r
const size_t down =\r
- (self->num_[key] > BLOCK_SIZE) ? (self->num_[key] - BLOCK_SIZE) : 0u;\r
- for (i = self->num_[key]; i > down;) {\r
- size_t prev_ix = bucket[--i & BLOCK_MASK];\r
+ (num[key] > self->block_size_) ? (num[key] - self->block_size_) : 0u;\r
+ for (i = num[key]; i > down;) {\r
+ size_t prev_ix = bucket[--i & self->block_mask_];\r
const size_t backward = cur_ix - prev_ix;\r
- if (PREDICT_FALSE(backward > max_backward)) {\r
+ if (BROTLI_PREDICT_FALSE(backward > max_backward)) {\r
break;\r
}\r
prev_ix &= ring_buffer_mask;\r
out->len = best_len;\r
out->distance = backward;\r
out->score = best_score;\r
- is_match_found = BROTLI_TRUE;\r
}\r
}\r
}\r
}\r
- bucket[self->num_[key] & BLOCK_MASK] = (uint32_t)cur_ix;\r
- ++self->num_[key];\r
+ bucket[num[key] & self->block_mask_] = (uint32_t)cur_ix;\r
+ ++num[key];\r
}\r
- if (!is_match_found) {\r
- is_match_found = SearchInStaticDictionary(&self->dict_search_stats_,\r
- &data[cur_ix_masked], max_length, max_backward, out, BROTLI_FALSE);\r
+ if (min_score == out->score) {\r
+ SearchInStaticDictionary(dictionary,\r
+ handle, &data[cur_ix_masked], max_length, max_backward + gap,\r
+ max_distance, out, BROTLI_FALSE);\r
}\r
- return is_match_found;\r
}\r
\r
-#undef HASH_MAP_SIZE\r
-#undef BLOCK_MASK\r
-#undef BLOCK_SIZE\r
-#undef BUCKET_SIZE\r
-\r
#undef HashLongestMatch\r