Skip to content

Commit 4f41092

Browse files
authored
Turbopack: move block offsets from header to footer (#82047)
### What? move block offset map to the end of the file to enable streaming write of the SST file.
1 parent d49f449 commit 4f41092

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

turbopack/crates/turbo-persistence/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ The SST file contains only data without any header.
6060

6161
- serialized key Compression Dictionary
6262
- serialized value Compression Dictionary
63-
- foreach block
64-
- 4 bytes end of block offset relative to start of all blocks
6563
- foreach block
6664
- 4 bytes uncompressed block length
6765
- compressed data
66+
- foreach block
67+
- 4 bytes end of block offset relative to start of all blocks
6868

6969
#### Index Block
7070

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ pub struct StaticSortedFileMetaData {
7474
}
7575

7676
impl StaticSortedFileMetaData {
77-
pub fn block_offsets_start(&self) -> usize {
78-
let k: usize = self.key_compression_dictionary_length.into();
79-
let v: usize = self.value_compression_dictionary_length.into();
80-
k + v
77+
pub fn block_offsets_start(&self, sst_len: usize) -> usize {
78+
let bc: usize = self.block_count.into();
79+
sst_len - (bc * size_of::<u32>())
8180
}
8281

8382
pub fn blocks_start(&self) -> usize {
84-
let bc: usize = self.block_count.into();
85-
self.block_offsets_start() + bc * size_of::<u32>()
83+
let k: usize = self.key_compression_dictionary_length.into();
84+
let v: usize = self.value_compression_dictionary_length.into();
85+
k + v
8686
}
8787

8888
pub fn key_compression_dictionary_range(&self) -> Range<usize> {
@@ -347,11 +347,11 @@ impl StaticSortedFile {
347347
self.meta.sequence_number,
348348
block_index,
349349
self.meta.block_count,
350-
self.meta.block_offsets_start(),
350+
self.meta.block_offsets_start(self.mmap.len()),
351351
self.meta.blocks_start()
352352
);
353353
}
354-
let offset = self.meta.block_offsets_start() + block_index as usize * 4;
354+
let offset = self.meta.block_offsets_start(self.mmap.len()) + block_index as usize * 4;
355355
#[cfg(feature = "strict_checks")]
356356
if offset + 4 > self.mmap.len() {
357357
bail!(
@@ -361,7 +361,7 @@ impl StaticSortedFile {
361361
block_index,
362362
offset,
363363
self.mmap.len(),
364-
self.meta.block_offsets_start(),
364+
self.meta.block_offsets_start(self.mmap.len()),
365365
self.meta.blocks_start()
366366
);
367367
}
@@ -382,7 +382,7 @@ impl StaticSortedFile {
382382
block_start,
383383
block_end,
384384
self.mmap.len(),
385-
self.meta.block_offsets_start(),
385+
self.meta.block_offsets_start(self.mmap.len()),
386386
self.meta.blocks_start()
387387
);
388388
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,20 +409,24 @@ impl<'a> StaticSortedFileBuilder<'a> {
409409
file.write_all(&self.value_compression_dictionary)?;
410410

411411
// Write the blocks
412+
let block_count = self.blocks.len().try_into().unwrap();
413+
let mut block_offsets = Vec::with_capacity(self.blocks.len());
412414
let mut offset = 0;
413-
for (_, block) in &self.blocks {
415+
for (uncompressed_size, block) in self.blocks {
414416
// Block length (including the uncompressed length field)
415417
let len = block.len() + 4;
416418
offset += len;
417-
file.write_u32::<BE>(offset.try_into().unwrap())?;
418-
}
419-
let block_count = self.blocks.len().try_into().unwrap();
420-
for (uncompressed_size, block) in self.blocks {
419+
block_offsets.push(offset.try_into().unwrap());
421420
// Uncompressed size
422421
file.write_u32::<BE>(uncompressed_size)?;
423422
// Compressed block
424423
file.write_all(&block)?;
425424
}
425+
// Write the block offsets
426+
for offset in block_offsets {
427+
file.write_u32::<BE>(offset)?;
428+
}
429+
426430
let meta = StaticSortedFileBuilderMeta {
427431
min_hash: self.min_hash,
428432
max_hash: self.max_hash,

0 commit comments

Comments
 (0)