Skip to content

Commit ef91691

Browse files
committed
avoid duplicate code in compression dictionary creation
1 parent 3831bd8 commit ef91691

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const AMQF_FALSE_POSITIVE_RATE: f64 = 0.01;
3535
const KEY_COMPRESSION_DICTIONARY_SIZE: usize = 64 * 1024 - 1;
3636
/// The maximum bytes that should be selected as key samples to create a compression dictionary
3737
const KEY_COMPRESSION_SAMPLES_SIZE: usize = 256 * 1024;
38-
/// The minimum bytes that should be selected as keys samples. Below that no compression dictionary
38+
/// 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;
4141
/// The bytes that are used per key entry for a sample.
@@ -147,12 +147,10 @@ pub fn write_static_stored_file<E: Entry>(
147147
}
148148

149149
fn get_compression_buffer_capacity(total_key_size: usize) -> usize {
150-
let mut size = 0;
151-
if total_key_size >= MIN_KEY_COMPRESSION_SAMPLES_SIZE {
152-
let key_compression_samples_size = min(KEY_COMPRESSION_SAMPLES_SIZE, total_key_size / 16);
153-
size = key_compression_samples_size;
150+
if total_key_size < MIN_KEY_COMPRESSION_SAMPLES_SIZE {
151+
return 0;
154152
}
155-
size
153+
min(KEY_COMPRESSION_SAMPLES_SIZE, total_key_size / 16)
156154
}
157155

158156
/// Computes compression dictionaries from keys of all entries
@@ -162,10 +160,11 @@ fn compute_key_compression_dictionary<E: Entry>(
162160
total_key_size: usize,
163161
buffer: &mut Vec<u8>,
164162
) -> Result<Vec<u8>> {
165-
if total_key_size < MIN_KEY_COMPRESSION_SAMPLES_SIZE {
163+
let key_compression_samples_size = get_compression_buffer_capacity(total_key_size);
164+
if key_compression_samples_size == 0 {
166165
return Ok(Vec::new());
167166
}
168-
let key_compression_samples_size = min(KEY_COMPRESSION_SAMPLES_SIZE, total_key_size / 16);
167+
169168
let mut sample_sizes = Vec::new();
170169

171170
// Limit the number of iterations to avoid infinite loops

0 commit comments

Comments
 (0)