Skip to content

Commit 678ef8b

Browse files
authored
perf(turbopack): Remove needless alloc for AQMF (#80468)
### What? Trivial `Cow` optimization for AQMF data ### Why? We can avoid calling `to_vec()` on this data, and according to the profiling result, compression was a memory hog.
1 parent ef2961c commit 678ef8b

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
any::{Any, TypeId},
3+
borrow::Cow,
34
collections::HashSet,
45
fs::{self, File, OpenOptions, ReadDir},
56
io::{BufWriter, Write},
@@ -1050,7 +1051,7 @@ impl TurboPersistence {
10501051
let index_in_meta = ssts_with_ranges[index].index_in_meta;
10511052
let meta_file = &meta_files[meta_index];
10521053
let entry = meta_file.entry(index_in_meta);
1053-
let aqmf = entry.raw_aqmf(meta_file.aqmf_data()).to_vec();
1054+
let aqmf = Cow::Borrowed(entry.raw_aqmf(meta_file.aqmf_data()));
10541055
let meta = StaticSortedFileBuilderMeta {
10551056
min_hash: entry.min_hash(),
10561057
max_hash: entry.max_hash(),

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ use byteorder::{BE, WriteBytesExt};
99

1010
use crate::static_sorted_file_builder::StaticSortedFileBuilderMeta;
1111

12-
pub struct MetaFileBuilder {
12+
pub struct MetaFileBuilder<'a> {
1313
family: u32,
1414
/// Entries in the meta file, tuples of (sequence_number, StaticSortedFileBuilderMetaResult)
15-
entries: Vec<(u32, StaticSortedFileBuilderMeta)>,
15+
entries: Vec<(u32, StaticSortedFileBuilderMeta<'a>)>,
1616
/// Obsolete SST files, represented by their sequence numbers
1717
obsolete_sst_files: Vec<u32>,
1818
}
1919

20-
impl MetaFileBuilder {
20+
impl<'a> MetaFileBuilder<'a> {
2121
pub fn new(family: u32) -> Self {
2222
Self {
2323
family,
@@ -26,7 +26,7 @@ impl MetaFileBuilder {
2626
}
2727
}
2828

29-
pub fn add(&mut self, sequence_number: u32, sst: StaticSortedFileBuilderMeta) {
29+
pub fn add(&mut self, sequence_number: u32, sst: StaticSortedFileBuilderMeta<'a>) {
3030
self.entries.push((sequence_number, sst));
3131
}
3232

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
borrow::Cow,
23
cmp::min,
34
fs::File,
45
io::{self, BufWriter, Seek, Write},
@@ -72,13 +73,13 @@ pub enum EntryValue<'l> {
7273
}
7374

7475
#[derive(Debug, Clone)]
75-
pub struct StaticSortedFileBuilderMeta {
76+
pub struct StaticSortedFileBuilderMeta<'a> {
7677
/// The minimum hash of the keys in the SST file
7778
pub min_hash: u64,
7879
/// The maximum hash of the keys in the SST file
7980
pub max_hash: u64,
8081
/// The AQMF data
81-
pub aqmf: Vec<u8>,
82+
pub aqmf: Cow<'a, [u8]>,
8283
/// The key compression dictionary
8384
pub key_compression_dictionary_length: u16,
8485
/// The value compression dictionary
@@ -92,8 +93,8 @@ pub struct StaticSortedFileBuilderMeta {
9293
}
9394

9495
#[derive(Debug, Default)]
95-
pub struct StaticSortedFileBuilder {
96-
aqmf: Vec<u8>,
96+
pub struct StaticSortedFileBuilder<'a> {
97+
aqmf: Cow<'a, [u8]>,
9798
key_compression_dictionary: Vec<u8>,
9899
value_compression_dictionary: Vec<u8>,
99100
blocks: Vec<(u32, Vec<u8>)>,
@@ -102,7 +103,7 @@ pub struct StaticSortedFileBuilder {
102103
entries: u64,
103104
}
104105

105-
impl StaticSortedFileBuilder {
106+
impl<'a> StaticSortedFileBuilder<'a> {
106107
pub fn new<E: Entry>(
107108
entries: &[E],
108109
total_key_size: usize,
@@ -136,7 +137,7 @@ impl StaticSortedFileBuilder {
136137
for entry in entries {
137138
debug_assert!(filter.contains_fingerprint(entry.key_hash()));
138139
}
139-
self.aqmf = pot::to_vec(&filter).expect("AQMF serialization failed");
140+
self.aqmf = Cow::Owned(pot::to_vec(&filter).expect("AQMF serialization failed"));
140141
}
141142

142143
/// Computes compression dictionaries from keys and values of all entries
@@ -389,7 +390,7 @@ impl StaticSortedFileBuilder {
389390

390391
/// Writes the SST file.
391392
#[tracing::instrument(level = "trace", skip_all)]
392-
pub fn write(self, file: &Path) -> io::Result<(StaticSortedFileBuilderMeta, File)> {
393+
pub fn write(self, file: &Path) -> io::Result<(StaticSortedFileBuilderMeta<'a>, File)> {
393394
let mut file = BufWriter::new(File::create(file)?);
394395
// Write the key compression dictionary
395396
file.write_all(&self.key_compression_dictionary)?;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct WriteBatch<K: StoreKey + Send, const FAMILIES: usize> {
7878
/// Collectors in use. The thread local collectors flush into these when they are full.
7979
collectors: [Mutex<GlobalCollectorState<K>>; FAMILIES],
8080
/// Meta file builders for each family.
81-
meta_collectors: [Mutex<Vec<(u32, StaticSortedFileBuilderMeta)>>; FAMILIES],
81+
meta_collectors: [Mutex<Vec<(u32, StaticSortedFileBuilderMeta<'static>)>>; FAMILIES],
8282
/// The list of new SST files that have been created.
8383
/// Tuple of (sequence number, file).
8484
new_sst_files: Mutex<Vec<(u32, File)>>,

0 commit comments

Comments
 (0)