Skip to content

Commit 0b45cdf

Browse files
committed
fixup compute_dictionary
1 parent e92c0ad commit 0b45cdf

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

turbopack/crates/turbo-persistence/src/static_sorted_file_builder.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ fn compute_key_compression_dictionary<E: Entry>(
163163
return Ok(Vec::new());
164164
}
165165

166+
let max_sample_size = max(
167+
MIN_COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY,
168+
key_compression_samples_size / 1024,
169+
);
170+
166171
let mut sample_sizes = Vec::new();
167172

168173
for entry in entries {
@@ -172,7 +177,8 @@ fn compute_key_compression_dictionary<E: Entry>(
172177
}
173178
let len = entry.key_len();
174179
if len >= MIN_COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY {
175-
let optimal_len = max(MIN_COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY, len / 8);
180+
let optimal_len =
181+
(len / 8).clamp(MIN_COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY, max_sample_size);
176182
let used_len = min(key_remaining, optimal_len);
177183
if len <= used_len {
178184
sample_sizes.push(len);
@@ -188,10 +194,12 @@ fn compute_key_compression_dictionary<E: Entry>(
188194
}
189195
}
190196
}
191-
debug_assert!(buffer.len() == sample_sizes.iter().sum::<usize>());
192-
let result = if buffer.len() > MIN_KEY_COMPRESSION_SAMPLES_SIZE && sample_sizes.len() > 5 {
193-
zstd::dict::from_continuous(buffer, &sample_sizes, KEY_COMPRESSION_DICTIONARY_SIZE)
194-
.context("Key dictionary creation failed")?
197+
/// The zlib dict builder requires at least 7 samples
198+
const MIN_SAMPLE_SIZE: usize = 7;
199+
let result = if buffer.len() > MIN_KEY_COMPRESSION_SAMPLES_SIZE
200+
&& sample_sizes.len() > MIN_SAMPLE_SIZE
201+
{
202+
zstd::dict::from_continuous(buffer, &sample_sizes, KEY_COMPRESSION_DICTIONARY_SIZE)?
195203
} else {
196204
Vec::new()
197205
};

0 commit comments

Comments
 (0)