Skip to content

Commit 3470c0a

Browse files
committed
avoid multiple iterations for compression dictionary
1 parent ef91691 commit 3470c0a

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
borrow::Cow,
3-
cmp::min,
3+
cmp::{max, min},
44
fs::File,
55
io::{BufWriter, Seek, Write},
66
path::Path,
@@ -38,8 +38,6 @@ const KEY_COMPRESSION_SAMPLES_SIZE: usize = 256 * 1024;
3838
/// The minimum bytes that should be selected as key samples. Below that no compression dictionary
3939
/// is used.
4040
const MIN_KEY_COMPRESSION_SAMPLES_SIZE: usize = 1024;
41-
/// The bytes that are used per key entry for a sample.
42-
const COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY: usize = 100;
4341
/// The minimum bytes that are used per key entry for a sample.
4442
const MIN_COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY: usize = 16;
4543

@@ -167,17 +165,15 @@ fn compute_key_compression_dictionary<E: Entry>(
167165

168166
let mut sample_sizes = Vec::new();
169167

170-
// Limit the number of iterations to avoid infinite loops
171-
let max_iterations = total_key_size / COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY * 2;
172-
for i in 0..max_iterations {
173-
let entry = &entries[i % entries.len()];
168+
for entry in entries {
174169
let key_remaining = key_compression_samples_size - buffer.len();
175170
if key_remaining < MIN_COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY {
176171
break;
177172
}
178173
let len = entry.key_len();
179174
if len >= MIN_COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY {
180-
let used_len = min(key_remaining, COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY);
175+
let optimal_len = max(MIN_COMPRESSION_DICTIONARY_SAMPLE_PER_ENTRY, len / 8);
176+
let used_len = min(key_remaining, optimal_len);
181177
if len <= used_len {
182178
sample_sizes.push(len);
183179
entry.write_key_to(buffer);

0 commit comments

Comments
 (0)