brotli 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +7 -3
- data/brotli.gemspec +1 -1
- data/ext/brotli/brotli.c +4 -4
- data/ext/brotli/brotli.h +2 -2
- data/ext/brotli/extconf.rb +9 -16
- data/lib/brotli/version.rb +1 -1
- data/vendor/brotli/{common → c/common}/constants.h +11 -1
- data/vendor/brotli/c/common/dictionary.bin +432 -0
- data/vendor/brotli/c/common/dictionary.c +5905 -0
- data/vendor/brotli/c/common/dictionary.h +64 -0
- data/vendor/brotli/c/common/version.h +19 -0
- data/vendor/brotli/{dec → c/dec}/bit_reader.c +2 -2
- data/vendor/brotli/{dec → c/dec}/bit_reader.h +11 -34
- data/vendor/brotli/{dec → c/dec}/context.h +1 -1
- data/vendor/brotli/{dec → c/dec}/decode.c +389 -356
- data/vendor/brotli/{dec → c/dec}/huffman.c +24 -23
- data/vendor/brotli/{dec → c/dec}/huffman.h +1 -1
- data/vendor/brotli/{dec → c/dec}/port.h +19 -10
- data/vendor/brotli/{dec → c/dec}/prefix.h +1 -1
- data/vendor/brotli/{dec → c/dec}/state.c +23 -19
- data/vendor/brotli/{dec → c/dec}/state.h +18 -17
- data/vendor/brotli/{dec → c/dec}/transform.h +2 -2
- data/vendor/brotli/c/enc/backward_references.c +134 -0
- data/vendor/brotli/c/enc/backward_references.h +39 -0
- data/vendor/brotli/{enc/backward_references.c → c/enc/backward_references_hq.c} +144 -232
- data/vendor/brotli/{enc/backward_references.h → c/enc/backward_references_hq.h} +28 -31
- data/vendor/brotli/{enc → c/enc}/backward_references_inc.h +37 -31
- data/vendor/brotli/{enc → c/enc}/bit_cost.c +1 -1
- data/vendor/brotli/{enc → c/enc}/bit_cost.h +1 -1
- data/vendor/brotli/{enc → c/enc}/bit_cost_inc.h +0 -0
- data/vendor/brotli/{enc → c/enc}/block_encoder_inc.h +0 -0
- data/vendor/brotli/{enc → c/enc}/block_splitter.c +2 -4
- data/vendor/brotli/{enc → c/enc}/block_splitter.h +1 -1
- data/vendor/brotli/{enc → c/enc}/block_splitter_inc.h +6 -7
- data/vendor/brotli/{enc → c/enc}/brotli_bit_stream.c +22 -26
- data/vendor/brotli/{enc → c/enc}/brotli_bit_stream.h +1 -5
- data/vendor/brotli/{enc → c/enc}/cluster.c +1 -1
- data/vendor/brotli/{enc → c/enc}/cluster.h +1 -1
- data/vendor/brotli/{enc → c/enc}/cluster_inc.h +2 -0
- data/vendor/brotli/{enc → c/enc}/command.h +34 -17
- data/vendor/brotli/{enc → c/enc}/compress_fragment.c +97 -53
- data/vendor/brotli/{enc → c/enc}/compress_fragment.h +5 -2
- data/vendor/brotli/{enc → c/enc}/compress_fragment_two_pass.c +106 -51
- data/vendor/brotli/{enc → c/enc}/compress_fragment_two_pass.h +5 -2
- data/vendor/brotli/{enc → c/enc}/context.h +3 -3
- data/vendor/brotli/c/enc/dictionary_hash.c +1120 -0
- data/vendor/brotli/c/enc/dictionary_hash.h +24 -0
- data/vendor/brotli/{enc → c/enc}/encode.c +442 -240
- data/vendor/brotli/{enc → c/enc}/entropy_encode.c +9 -9
- data/vendor/brotli/{enc → c/enc}/entropy_encode.h +4 -4
- data/vendor/brotli/{enc → c/enc}/entropy_encode_static.h +4 -4
- data/vendor/brotli/{enc → c/enc}/fast_log.h +3 -3
- data/vendor/brotli/{enc → c/enc}/find_match_length.h +8 -8
- data/vendor/brotli/c/enc/hash.h +446 -0
- data/vendor/brotli/{enc → c/enc}/hash_forgetful_chain_inc.h +72 -68
- data/vendor/brotli/c/enc/hash_longest_match64_inc.h +266 -0
- data/vendor/brotli/c/enc/hash_longest_match_inc.h +258 -0
- data/vendor/brotli/{enc → c/enc}/hash_longest_match_quickly_inc.h +81 -77
- data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +326 -0
- data/vendor/brotli/{enc → c/enc}/histogram.c +4 -2
- data/vendor/brotli/{enc → c/enc}/histogram.h +1 -1
- data/vendor/brotli/{enc → c/enc}/histogram_inc.h +0 -0
- data/vendor/brotli/{enc → c/enc}/literal_cost.c +4 -7
- data/vendor/brotli/{enc → c/enc}/literal_cost.h +2 -2
- data/vendor/brotli/{enc → c/enc}/memory.c +1 -1
- data/vendor/brotli/{enc → c/enc}/memory.h +3 -2
- data/vendor/brotli/{enc → c/enc}/metablock.c +136 -123
- data/vendor/brotli/{enc → c/enc}/metablock.h +2 -12
- data/vendor/brotli/{enc → c/enc}/metablock_inc.h +0 -0
- data/vendor/brotli/{enc → c/enc}/port.h +49 -33
- data/vendor/brotli/{enc → c/enc}/prefix.h +4 -2
- data/vendor/brotli/{enc → c/enc}/quality.h +47 -17
- data/vendor/brotli/{enc → c/enc}/ringbuffer.h +6 -6
- data/vendor/brotli/{enc → c/enc}/static_dict.c +26 -22
- data/vendor/brotli/{enc → c/enc}/static_dict.h +3 -1
- data/vendor/brotli/c/enc/static_dict_lut.h +5864 -0
- data/vendor/brotli/{enc → c/enc}/utf8_util.c +1 -1
- data/vendor/brotli/{enc → c/enc}/utf8_util.h +2 -2
- data/vendor/brotli/{enc → c/enc}/write_bits.h +3 -3
- data/vendor/brotli/c/include/brotli/decode.h +339 -0
- data/vendor/brotli/c/include/brotli/encode.h +402 -0
- data/vendor/brotli/c/include/brotli/port.h +146 -0
- data/vendor/brotli/c/include/brotli/types.h +90 -0
- metadata +80 -79
- data/vendor/brotli/common/dictionary.c +0 -9474
- data/vendor/brotli/common/dictionary.h +0 -29
- data/vendor/brotli/common/port.h +0 -107
- data/vendor/brotli/common/types.h +0 -58
- data/vendor/brotli/dec/decode.h +0 -188
- data/vendor/brotli/enc/compressor.cc +0 -139
- data/vendor/brotli/enc/compressor.h +0 -161
- data/vendor/brotli/enc/dictionary_hash.h +0 -4121
- data/vendor/brotli/enc/encode.h +0 -221
- data/vendor/brotli/enc/encode_parallel.cc +0 -289
- data/vendor/brotli/enc/encode_parallel.h +0 -27
- data/vendor/brotli/enc/hash.h +0 -717
- data/vendor/brotli/enc/hash_longest_match_inc.h +0 -241
- data/vendor/brotli/enc/static_dict_lut.h +0 -11241
- data/vendor/brotli/enc/streams.cc +0 -114
- data/vendor/brotli/enc/streams.h +0 -121
@@ -0,0 +1,258 @@
|
|
1
|
+
/* NOLINT(build/header_guard) */
|
2
|
+
/* Copyright 2010 Google Inc. All Rights Reserved.
|
3
|
+
|
4
|
+
Distributed under MIT license.
|
5
|
+
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
6
|
+
*/
|
7
|
+
|
8
|
+
/* template parameters: FN */
|
9
|
+
|
10
|
+
/* A (forgetful) hash table to the data seen by the compressor, to
|
11
|
+
help create backward references to previous data.
|
12
|
+
|
13
|
+
This is a hash map of fixed size (bucket_size_) to a ring buffer of
|
14
|
+
fixed size (block_size_). The ring buffer contains the last block_size_
|
15
|
+
index positions of the given hash key in the compressed data. */
|
16
|
+
|
17
|
+
#define HashLongestMatch HASHER()
|
18
|
+
|
19
|
+
static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 4; }
|
20
|
+
static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 4; }
|
21
|
+
|
22
|
+
/* HashBytes is the function that chooses the bucket to place the address in. */
|
23
|
+
static uint32_t FN(HashBytes)(const uint8_t *data, const int shift) {
|
24
|
+
uint32_t h = BROTLI_UNALIGNED_LOAD32(data) * kHashMul32;
|
25
|
+
/* The higher bits contain more mixture from the multiplication,
|
26
|
+
so we take our results from there. */
|
27
|
+
return (uint32_t)(h >> shift);
|
28
|
+
}
|
29
|
+
|
30
|
+
typedef struct HashLongestMatch {
|
31
|
+
/* Number of hash buckets. */
|
32
|
+
size_t bucket_size_;
|
33
|
+
/* Only block_size_ newest backward references are kept,
|
34
|
+
and the older are forgotten. */
|
35
|
+
size_t block_size_;
|
36
|
+
/* Left-shift for computing hash bucket index from hash value. */
|
37
|
+
int hash_shift_;
|
38
|
+
/* Mask for accessing entries in a block (in a ring-buffer manner). */
|
39
|
+
uint32_t block_mask_;
|
40
|
+
|
41
|
+
/* --- Dynamic size members --- */
|
42
|
+
|
43
|
+
/* Number of entries in a particular bucket. */
|
44
|
+
/* uint16_t num[bucket_size]; */
|
45
|
+
|
46
|
+
/* Buckets containing block_size_ of backward references. */
|
47
|
+
/* uint32_t* buckets[bucket_size * block_size]; */
|
48
|
+
} HashLongestMatch;
|
49
|
+
|
50
|
+
static BROTLI_INLINE HashLongestMatch* FN(Self)(HasherHandle handle) {
|
51
|
+
return (HashLongestMatch*)&(GetHasherCommon(handle)[1]);
|
52
|
+
}
|
53
|
+
|
54
|
+
static BROTLI_INLINE uint16_t* FN(Num)(HashLongestMatch* self) {
|
55
|
+
return (uint16_t*)(&self[1]);
|
56
|
+
}
|
57
|
+
|
58
|
+
static BROTLI_INLINE uint32_t* FN(Buckets)(HashLongestMatch* self) {
|
59
|
+
return (uint32_t*)(&FN(Num)(self)[self->bucket_size_]);
|
60
|
+
}
|
61
|
+
|
62
|
+
static void FN(Initialize)(
|
63
|
+
HasherHandle handle, const BrotliEncoderParams* params) {
|
64
|
+
HasherCommon* common = GetHasherCommon(handle);
|
65
|
+
HashLongestMatch* self = FN(Self)(handle);
|
66
|
+
BROTLI_UNUSED(params);
|
67
|
+
self->hash_shift_ = 32 - common->params.bucket_bits;
|
68
|
+
self->bucket_size_ = (size_t)1 << common->params.bucket_bits;
|
69
|
+
self->block_size_ = (size_t)1 << common->params.block_bits;
|
70
|
+
self->block_mask_ = (uint32_t)(self->block_size_ - 1);
|
71
|
+
}
|
72
|
+
|
73
|
+
static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,
|
74
|
+
size_t input_size, const uint8_t* data) {
|
75
|
+
HashLongestMatch* self = FN(Self)(handle);
|
76
|
+
uint16_t* num = FN(Num)(self);
|
77
|
+
/* Partial preparation is 100 times slower (per socket). */
|
78
|
+
size_t partial_prepare_threshold = self->bucket_size_ >> 6;
|
79
|
+
if (one_shot && input_size <= partial_prepare_threshold) {
|
80
|
+
size_t i;
|
81
|
+
for (i = 0; i < input_size; ++i) {
|
82
|
+
const uint32_t key = FN(HashBytes)(&data[i], self->hash_shift_);
|
83
|
+
num[key] = 0;
|
84
|
+
}
|
85
|
+
} else {
|
86
|
+
memset(num, 0, self->bucket_size_ * sizeof(num[0]));
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(
|
91
|
+
const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
|
92
|
+
size_t input_size) {
|
93
|
+
size_t bucket_size = (size_t)1 << params->hasher.bucket_bits;
|
94
|
+
size_t block_size = (size_t)1 << params->hasher.block_bits;
|
95
|
+
BROTLI_UNUSED(one_shot);
|
96
|
+
BROTLI_UNUSED(input_size);
|
97
|
+
return sizeof(HashLongestMatch) + bucket_size * (2 + 4 * block_size);
|
98
|
+
}
|
99
|
+
|
100
|
+
/* Look at 4 bytes at &data[ix & mask].
|
101
|
+
Compute a hash from these, and store the value of ix at that position. */
|
102
|
+
static BROTLI_INLINE void FN(Store)(HasherHandle handle, const uint8_t* data,
|
103
|
+
const size_t mask, const size_t ix) {
|
104
|
+
HashLongestMatch* self = FN(Self)(handle);
|
105
|
+
uint16_t* num = FN(Num)(self);
|
106
|
+
const uint32_t key = FN(HashBytes)(&data[ix & mask], self->hash_shift_);
|
107
|
+
const size_t minor_ix = num[key] & self->block_mask_;
|
108
|
+
const size_t offset =
|
109
|
+
minor_ix + (key << GetHasherCommon(handle)->params.block_bits);
|
110
|
+
FN(Buckets)(self)[offset] = (uint32_t)ix;
|
111
|
+
++num[key];
|
112
|
+
}
|
113
|
+
|
114
|
+
static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,
|
115
|
+
const uint8_t *data, const size_t mask, const size_t ix_start,
|
116
|
+
const size_t ix_end) {
|
117
|
+
size_t i;
|
118
|
+
for (i = ix_start; i < ix_end; ++i) {
|
119
|
+
FN(Store)(handle, data, mask, i);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle,
|
124
|
+
size_t num_bytes, size_t position, const uint8_t* ringbuffer,
|
125
|
+
size_t ringbuffer_mask) {
|
126
|
+
if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
|
127
|
+
/* Prepare the hashes for three last bytes of the last write.
|
128
|
+
These could not be calculated before, since they require knowledge
|
129
|
+
of both the previous and the current block. */
|
130
|
+
FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 3);
|
131
|
+
FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 2);
|
132
|
+
FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 1);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
|
136
|
+
static BROTLI_INLINE void FN(PrepareDistanceCache)(
|
137
|
+
HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {
|
138
|
+
PrepareDistanceCache(distance_cache,
|
139
|
+
GetHasherCommon(handle)->params.num_last_distances_to_check);
|
140
|
+
}
|
141
|
+
|
142
|
+
/* Find a longest backward match of &data[cur_ix] up to the length of
|
143
|
+
max_length and stores the position cur_ix in the hash table.
|
144
|
+
|
145
|
+
REQUIRES: FN(PrepareDistanceCache) must be invoked for current distance cache
|
146
|
+
values; if this method is invoked repeatedly with the same distance
|
147
|
+
cache values, it is enough to invoke FN(PrepareDistanceCache) once.
|
148
|
+
|
149
|
+
Does not look for matches longer than max_length.
|
150
|
+
Does not look for matches further away than max_backward.
|
151
|
+
Writes the best match into |out|.
|
152
|
+
|out|->score is updated only if a better match is found. */
|
153
|
+
static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle,
|
154
|
+
const BrotliDictionary* dictionary, const uint16_t* dictionary_hash,
|
155
|
+
const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
|
156
|
+
const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
|
157
|
+
const size_t max_length, const size_t max_backward, const size_t gap,
|
158
|
+
HasherSearchResult* BROTLI_RESTRICT out) {
|
159
|
+
HasherCommon* common = GetHasherCommon(handle);
|
160
|
+
HashLongestMatch* self = FN(Self)(handle);
|
161
|
+
uint16_t* num = FN(Num)(self);
|
162
|
+
uint32_t* buckets = FN(Buckets)(self);
|
163
|
+
const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
|
164
|
+
/* Don't accept a short copy from far away. */
|
165
|
+
score_t min_score = out->score;
|
166
|
+
score_t best_score = out->score;
|
167
|
+
size_t best_len = out->len;
|
168
|
+
size_t i;
|
169
|
+
out->len = 0;
|
170
|
+
out->len_code_delta = 0;
|
171
|
+
/* Try last distance first. */
|
172
|
+
for (i = 0; i < (size_t)common->params.num_last_distances_to_check; ++i) {
|
173
|
+
const size_t backward = (size_t)distance_cache[i];
|
174
|
+
size_t prev_ix = (size_t)(cur_ix - backward);
|
175
|
+
if (prev_ix >= cur_ix) {
|
176
|
+
continue;
|
177
|
+
}
|
178
|
+
if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
|
179
|
+
continue;
|
180
|
+
}
|
181
|
+
prev_ix &= ring_buffer_mask;
|
182
|
+
|
183
|
+
if (cur_ix_masked + best_len > ring_buffer_mask ||
|
184
|
+
prev_ix + best_len > ring_buffer_mask ||
|
185
|
+
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
|
186
|
+
continue;
|
187
|
+
}
|
188
|
+
{
|
189
|
+
const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
|
190
|
+
&data[cur_ix_masked],
|
191
|
+
max_length);
|
192
|
+
if (len >= 3 || (len == 2 && i < 2)) {
|
193
|
+
/* Comparing for >= 2 does not change the semantics, but just saves for
|
194
|
+
a few unnecessary binary logarithms in backward reference score,
|
195
|
+
since we are not interested in such short matches. */
|
196
|
+
score_t score = BackwardReferenceScoreUsingLastDistance(len);
|
197
|
+
if (best_score < score) {
|
198
|
+
if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
|
199
|
+
if (best_score < score) {
|
200
|
+
best_score = score;
|
201
|
+
best_len = len;
|
202
|
+
out->len = best_len;
|
203
|
+
out->distance = backward;
|
204
|
+
out->score = best_score;
|
205
|
+
}
|
206
|
+
}
|
207
|
+
}
|
208
|
+
}
|
209
|
+
}
|
210
|
+
{
|
211
|
+
const uint32_t key =
|
212
|
+
FN(HashBytes)(&data[cur_ix_masked], self->hash_shift_);
|
213
|
+
uint32_t* BROTLI_RESTRICT bucket =
|
214
|
+
&buckets[key << common->params.block_bits];
|
215
|
+
const size_t down =
|
216
|
+
(num[key] > self->block_size_) ? (num[key] - self->block_size_) : 0u;
|
217
|
+
for (i = num[key]; i > down;) {
|
218
|
+
size_t prev_ix = bucket[--i & self->block_mask_];
|
219
|
+
const size_t backward = cur_ix - prev_ix;
|
220
|
+
if (BROTLI_PREDICT_FALSE(backward > max_backward)) {
|
221
|
+
break;
|
222
|
+
}
|
223
|
+
prev_ix &= ring_buffer_mask;
|
224
|
+
if (cur_ix_masked + best_len > ring_buffer_mask ||
|
225
|
+
prev_ix + best_len > ring_buffer_mask ||
|
226
|
+
data[cur_ix_masked + best_len] != data[prev_ix + best_len]) {
|
227
|
+
continue;
|
228
|
+
}
|
229
|
+
{
|
230
|
+
const size_t len = FindMatchLengthWithLimit(&data[prev_ix],
|
231
|
+
&data[cur_ix_masked],
|
232
|
+
max_length);
|
233
|
+
if (len >= 4) {
|
234
|
+
/* Comparing for >= 3 does not change the semantics, but just saves
|
235
|
+
for a few unnecessary binary logarithms in backward reference
|
236
|
+
score, since we are not interested in such short matches. */
|
237
|
+
score_t score = BackwardReferenceScore(len, backward);
|
238
|
+
if (best_score < score) {
|
239
|
+
best_score = score;
|
240
|
+
best_len = len;
|
241
|
+
out->len = best_len;
|
242
|
+
out->distance = backward;
|
243
|
+
out->score = best_score;
|
244
|
+
}
|
245
|
+
}
|
246
|
+
}
|
247
|
+
}
|
248
|
+
bucket[num[key] & self->block_mask_] = (uint32_t)cur_ix;
|
249
|
+
++num[key];
|
250
|
+
}
|
251
|
+
if (min_score == out->score) {
|
252
|
+
SearchInStaticDictionary(dictionary, dictionary_hash,
|
253
|
+
handle, &data[cur_ix_masked], max_length, max_backward + gap, out,
|
254
|
+
BROTLI_FALSE);
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
#undef HashLongestMatch
|
@@ -5,7 +5,9 @@
|
|
5
5
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
6
6
|
*/
|
7
7
|
|
8
|
-
/* template parameters: FN, BUCKET_BITS, BUCKET_SWEEP,
|
8
|
+
/* template parameters: FN, BUCKET_BITS, BUCKET_SWEEP, HASH_LEN,
|
9
|
+
USE_DICTIONARY
|
10
|
+
*/
|
9
11
|
|
10
12
|
#define HashLongestMatchQuickly HASHER()
|
11
13
|
|
@@ -19,10 +21,9 @@ static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
|
|
19
21
|
/* HashBytes is the function that chooses the bucket to place
|
20
22
|
the address in. The HashLongestMatch and HashLongestMatchQuickly
|
21
23
|
classes have separate, different implementations of hashing. */
|
22
|
-
static uint32_t FN(HashBytes)(const uint8_t
|
23
|
-
|
24
|
-
|
25
|
-
uint64_t h = (BROTLI_UNALIGNED_LOAD64(data) << 24) * kHashMul32;
|
24
|
+
static uint32_t FN(HashBytes)(const uint8_t* data) {
|
25
|
+
const uint64_t h = ((BROTLI_UNALIGNED_LOAD64LE(data) << (64 - 8 * HASH_LEN)) *
|
26
|
+
kHashMul64);
|
26
27
|
/* The higher bits contain more mixture from the multiplication,
|
27
28
|
so we take our results from there. */
|
28
29
|
return (uint32_t)(h >> (64 - BUCKET_BITS));
|
@@ -35,88 +36,86 @@ static uint32_t FN(HashBytes)(const uint8_t *data) {
|
|
35
36
|
given index, BUCKET_SWEEP buckets are used to store values of a key. */
|
36
37
|
typedef struct HashLongestMatchQuickly {
|
37
38
|
uint32_t buckets_[BUCKET_SIZE + BUCKET_SWEEP];
|
38
|
-
/* True if buckets_ array needs to be initialized. */
|
39
|
-
BROTLI_BOOL is_dirty_;
|
40
|
-
DictionarySearchStatictics dict_search_stats_;
|
41
39
|
} HashLongestMatchQuickly;
|
42
40
|
|
43
|
-
static
|
44
|
-
|
45
|
-
DictionarySearchStaticticsReset(&self->dict_search_stats_);
|
41
|
+
static BROTLI_INLINE HashLongestMatchQuickly* FN(Self)(HasherHandle handle) {
|
42
|
+
return (HashLongestMatchQuickly*)&(GetHasherCommon(handle)[1]);
|
46
43
|
}
|
47
44
|
|
48
|
-
static void FN(
|
49
|
-
|
45
|
+
static void FN(Initialize)(
|
46
|
+
HasherHandle handle, const BrotliEncoderParams* params) {
|
47
|
+
BROTLI_UNUSED(handle);
|
48
|
+
BROTLI_UNUSED(params);
|
49
|
+
}
|
50
|
+
|
51
|
+
static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,
|
52
|
+
size_t input_size, const uint8_t* data) {
|
53
|
+
HashLongestMatchQuickly* self = FN(Self)(handle);
|
54
|
+
/* Partial preparation is 100 times slower (per socket). */
|
55
|
+
size_t partial_prepare_threshold = HASH_MAP_SIZE >> 7;
|
56
|
+
if (one_shot && input_size <= partial_prepare_threshold) {
|
57
|
+
size_t i;
|
58
|
+
for (i = 0; i < input_size; ++i) {
|
59
|
+
const uint32_t key = FN(HashBytes)(&data[i]);
|
60
|
+
memset(&self->buckets_[key], 0, BUCKET_SWEEP * sizeof(self->buckets_[0]));
|
61
|
+
}
|
62
|
+
} else {
|
50
63
|
/* It is not strictly necessary to fill this buffer here, but
|
51
64
|
not filling will make the results of the compression stochastic
|
52
65
|
(but correct). This is because random data would cause the
|
53
66
|
system to find accidentally good backward references here and there. */
|
54
67
|
memset(&self->buckets_[0], 0, sizeof(self->buckets_));
|
55
|
-
self->is_dirty_ = BROTLI_FALSE;
|
56
68
|
}
|
57
69
|
}
|
58
70
|
|
59
|
-
static
|
60
|
-
|
61
|
-
|
62
|
-
for (i = 0; i < num; ++i) {
|
63
|
-
const uint32_t key = FN(HashBytes)(&data[i]);
|
64
|
-
memset(&self->buckets_[key], 0, BUCKET_SWEEP * sizeof(self->buckets_[0]));
|
65
|
-
}
|
66
|
-
if (num != 0) {
|
67
|
-
self->is_dirty_ = BROTLI_FALSE;
|
68
|
-
}
|
69
|
-
}
|
70
|
-
|
71
|
-
static void FN(Init)(
|
72
|
-
MemoryManager* m, HashLongestMatchQuickly* self, const uint8_t* data,
|
73
|
-
const BrotliEncoderParams* params, size_t position, size_t bytes,
|
74
|
-
BROTLI_BOOL is_last) {
|
75
|
-
/* Choose which init method is faster.
|
76
|
-
Init() is about 100 times faster than InitForData(). */
|
77
|
-
const size_t kMaxBytesForPartialHashInit = HASH_MAP_SIZE >> 7;
|
78
|
-
BROTLI_UNUSED(m);
|
71
|
+
static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(
|
72
|
+
const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
|
73
|
+
size_t input_size) {
|
79
74
|
BROTLI_UNUSED(params);
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
FN(InitEmpty)(self);
|
84
|
-
}
|
75
|
+
BROTLI_UNUSED(one_shot);
|
76
|
+
BROTLI_UNUSED(input_size);
|
77
|
+
return sizeof(HashLongestMatchQuickly);
|
85
78
|
}
|
86
79
|
|
87
80
|
/* Look at 5 bytes at &data[ix & mask].
|
88
81
|
Compute a hash from these, and store the value somewhere within
|
89
82
|
[ix .. ix+3]. */
|
90
|
-
static BROTLI_INLINE void FN(Store)(
|
83
|
+
static BROTLI_INLINE void FN(Store)(HasherHandle handle,
|
91
84
|
const uint8_t *data, const size_t mask, const size_t ix) {
|
92
85
|
const uint32_t key = FN(HashBytes)(&data[ix & mask]);
|
93
86
|
/* Wiggle the value with the bucket sweep range. */
|
94
87
|
const uint32_t off = (ix >> 3) % BUCKET_SWEEP;
|
95
|
-
|
88
|
+
FN(Self)(handle)->buckets_[key + off] = (uint32_t)ix;
|
96
89
|
}
|
97
90
|
|
98
|
-
static BROTLI_INLINE void FN(StoreRange)(
|
91
|
+
static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,
|
99
92
|
const uint8_t *data, const size_t mask, const size_t ix_start,
|
100
93
|
const size_t ix_end) {
|
101
94
|
size_t i;
|
102
95
|
for (i = ix_start; i < ix_end; ++i) {
|
103
|
-
FN(Store)(
|
96
|
+
FN(Store)(handle, data, mask, i);
|
104
97
|
}
|
105
98
|
}
|
106
99
|
|
107
100
|
static BROTLI_INLINE void FN(StitchToPreviousBlock)(
|
108
|
-
|
101
|
+
HasherHandle handle, size_t num_bytes, size_t position,
|
109
102
|
const uint8_t* ringbuffer, size_t ringbuffer_mask) {
|
110
103
|
if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
|
111
104
|
/* Prepare the hashes for three last bytes of the last write.
|
112
105
|
These could not be calculated before, since they require knowledge
|
113
106
|
of both the previous and the current block. */
|
114
|
-
FN(Store)(
|
115
|
-
FN(Store)(
|
116
|
-
FN(Store)(
|
107
|
+
FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 3);
|
108
|
+
FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 2);
|
109
|
+
FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 1);
|
117
110
|
}
|
118
111
|
}
|
119
112
|
|
113
|
+
static BROTLI_INLINE void FN(PrepareDistanceCache)(
|
114
|
+
HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {
|
115
|
+
BROTLI_UNUSED(handle);
|
116
|
+
BROTLI_UNUSED(distance_cache);
|
117
|
+
}
|
118
|
+
|
120
119
|
/* Find a longest backward match of &data[cur_ix & ring_buffer_mask]
|
121
120
|
up to the length of max_length and stores the position cur_ix in the
|
122
121
|
hash table.
|
@@ -124,22 +123,24 @@ static BROTLI_INLINE void FN(StitchToPreviousBlock)(
|
|
124
123
|
Does not look for matches longer than max_length.
|
125
124
|
Does not look for matches further away than max_backward.
|
126
125
|
Writes the best match into |out|.
|
127
|
-
|
128
|
-
static BROTLI_INLINE
|
129
|
-
|
126
|
+
|out|->score is updated only if a better match is found. */
|
127
|
+
static BROTLI_INLINE void FN(FindLongestMatch)(
|
128
|
+
HasherHandle handle, const BrotliDictionary* dictionary,
|
129
|
+
const uint16_t* dictionary_hash, const uint8_t* BROTLI_RESTRICT data,
|
130
130
|
const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
|
131
131
|
const size_t cur_ix, const size_t max_length, const size_t max_backward,
|
132
|
-
HasherSearchResult* BROTLI_RESTRICT out) {
|
132
|
+
const size_t gap, HasherSearchResult* BROTLI_RESTRICT out) {
|
133
|
+
HashLongestMatchQuickly* self = FN(Self)(handle);
|
133
134
|
const size_t best_len_in = out->len;
|
134
135
|
const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
|
135
136
|
const uint32_t key = FN(HashBytes)(&data[cur_ix_masked]);
|
136
137
|
int compare_char = data[cur_ix_masked + best_len_in];
|
138
|
+
score_t min_score = out->score;
|
137
139
|
score_t best_score = out->score;
|
138
140
|
size_t best_len = best_len_in;
|
139
141
|
size_t cached_backward = (size_t)distance_cache[0];
|
140
142
|
size_t prev_ix = cur_ix - cached_backward;
|
141
|
-
|
142
|
-
out->len_x_code = 0;
|
143
|
+
out->len_code_delta = 0;
|
143
144
|
if (prev_ix < cur_ix) {
|
144
145
|
prev_ix &= (uint32_t)ring_buffer_mask;
|
145
146
|
if (compare_char == data[prev_ix + best_len]) {
|
@@ -147,17 +148,18 @@ static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(
|
|
147
148
|
&data[cur_ix_masked],
|
148
149
|
max_length);
|
149
150
|
if (len >= 4) {
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
151
|
+
const score_t score = BackwardReferenceScoreUsingLastDistance(len);
|
152
|
+
if (best_score < score) {
|
153
|
+
best_score = score;
|
154
|
+
best_len = len;
|
155
|
+
out->len = len;
|
156
|
+
out->distance = cached_backward;
|
157
|
+
out->score = best_score;
|
158
|
+
compare_char = data[cur_ix_masked + best_len];
|
159
|
+
if (BUCKET_SWEEP == 1) {
|
160
|
+
self->buckets_[key] = (uint32_t)cur_ix;
|
161
|
+
return;
|
162
|
+
}
|
161
163
|
}
|
162
164
|
}
|
163
165
|
}
|
@@ -171,19 +173,22 @@ static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(
|
|
171
173
|
backward = cur_ix - prev_ix;
|
172
174
|
prev_ix &= (uint32_t)ring_buffer_mask;
|
173
175
|
if (compare_char != data[prev_ix + best_len_in]) {
|
174
|
-
return
|
176
|
+
return;
|
175
177
|
}
|
176
|
-
if (
|
177
|
-
return
|
178
|
+
if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
|
179
|
+
return;
|
178
180
|
}
|
179
181
|
len = FindMatchLengthWithLimit(&data[prev_ix],
|
180
182
|
&data[cur_ix_masked],
|
181
183
|
max_length);
|
182
184
|
if (len >= 4) {
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
185
|
+
const score_t score = BackwardReferenceScore(len, backward);
|
186
|
+
if (best_score < score) {
|
187
|
+
out->len = len;
|
188
|
+
out->distance = backward;
|
189
|
+
out->score = score;
|
190
|
+
return;
|
191
|
+
}
|
187
192
|
}
|
188
193
|
} else {
|
189
194
|
uint32_t *bucket = self->buckets_ + key;
|
@@ -196,7 +201,7 @@ static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(
|
|
196
201
|
if (compare_char != data[prev_ix + best_len]) {
|
197
202
|
continue;
|
198
203
|
}
|
199
|
-
if (
|
204
|
+
if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
|
200
205
|
continue;
|
201
206
|
}
|
202
207
|
len = FindMatchLengthWithLimit(&data[prev_ix],
|
@@ -211,17 +216,16 @@ static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(
|
|
211
216
|
out->distance = backward;
|
212
217
|
out->score = score;
|
213
218
|
compare_char = data[cur_ix_masked + best_len];
|
214
|
-
is_match_found = BROTLI_TRUE;
|
215
219
|
}
|
216
220
|
}
|
217
221
|
}
|
218
222
|
}
|
219
|
-
if (USE_DICTIONARY &&
|
220
|
-
|
221
|
-
&data[cur_ix_masked], max_length, max_backward, out,
|
223
|
+
if (USE_DICTIONARY && min_score == out->score) {
|
224
|
+
SearchInStaticDictionary(dictionary, dictionary_hash,
|
225
|
+
handle, &data[cur_ix_masked], max_length, max_backward + gap, out,
|
226
|
+
BROTLI_TRUE);
|
222
227
|
}
|
223
228
|
self->buckets_[key + ((cur_ix >> 3) % BUCKET_SWEEP)] = (uint32_t)cur_ix;
|
224
|
-
return is_match_found;
|
225
229
|
}
|
226
230
|
|
227
231
|
#undef HASH_MAP_SIZE
|