Skip to content

Commit fbe6c26

Browse files
ampampcode-com
andcommitted
perf(provider): refresh static-file jars lazily
Use the in-memory segment header to update static-file indexes and only reopen the jar when that range is already cached by an active reader. This keeps save-block index maintenance on the write path from eagerly reloading and remapping jars that can be loaded lazily on the next read. Amp-Thread-ID: https://ampcode.com/threads/T-019de235-768d-731d-9a59-cf08487d9e84 Co-authored-by: Amp <[email protected]>
1 parent 8d7c15f commit fbe6c26

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

crates/storage/provider/src/providers/static_file/manager.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
10401040
&self,
10411041
segment: StaticFileSegment,
10421042
segment_max_block: Option<BlockNumber>,
1043+
header: Option<&SegmentHeader>,
10431044
) -> ProviderResult<()> {
10441045
debug!(
10451046
target: "providers::static_file",
@@ -1056,11 +1057,8 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
10561057
indexes.get(segment).map(|index| &index.expected_block_ranges_by_max_block),
10571058
segment_max_block,
10581059
);
1059-
1060-
let jar = NippyJar::<SegmentHeader>::load(
1061-
&self.path.join(segment.filename(&fixed_range)),
1062-
)
1063-
.map_err(ProviderError::other)?;
1060+
let current_block_range = header.and_then(SegmentHeader::block_range);
1061+
let tx_range = header.and_then(SegmentHeader::tx_range);
10641062

10651063
let index = indexes
10661064
.entry(segment)
@@ -1104,7 +1102,7 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
11041102
// 2. Sync to block 100, this update sets min_block = [0..=100]
11051103
// 3. Pruner calls get_lowest_static_file_block() -> returns 100 (correct). Without
11061104
// this update, it would incorrectly return 0 (stale)
1107-
if let Some(current_block_range) = jar.user_header().block_range() {
1105+
if let Some(current_block_range) = current_block_range {
11081106
if let Some(min_block_range) = index.min_block_range.as_mut() {
11091107
// delete_jar WILL ALWAYS re-initialize all indexes, so we are always
11101108
// sure that current_min is always the lowest.
@@ -1118,10 +1116,10 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
11181116

11191117
// Updates the tx index by first removing all entries which have a higher
11201118
// block_start than our current static file.
1121-
if let Some(tx_range) = jar.user_header().tx_range() {
1119+
if let Some(tx_range) = tx_range {
11221120
// Current block range has the same block start as `fixed_range``, but block end
11231121
// might be different if we are still filling this static file.
1124-
if let Some(current_block_range) = jar.user_header().block_range() {
1122+
if let Some(current_block_range) = current_block_range {
11251123
let tx_end = tx_range.end();
11261124

11271125
// Considering that `update_index` is called when we either append/truncate,
@@ -1153,9 +1151,16 @@ impl<N: NodePrimitives> StaticFileProvider<N> {
11531151
index.available_block_ranges_by_max_tx.take_if(|index| index.is_empty());
11541152
}
11551153

1156-
// Update the cached provider.
1157-
debug!(target: "providers::static_file", ?segment, "Inserting updated jar into cache");
1158-
self.map.insert((fixed_range.end(), segment), LoadedJar::new(jar)?);
1154+
// Keep writer-side index updates cheap by only reopening the jar when a reader
1155+
// already has this range cached. Otherwise the next read will lazily load it.
1156+
if self.map.contains_key(&(fixed_range.end(), segment)) {
1157+
let jar = NippyJar::<SegmentHeader>::load(
1158+
&self.path.join(segment.filename(&fixed_range)),
1159+
)
1160+
.map_err(ProviderError::other)?;
1161+
debug!(target: "providers::static_file", ?segment, "Inserting updated jar into cache");
1162+
self.map.insert((fixed_range.end(), segment), LoadedJar::new(jar)?);
1163+
}
11591164

11601165
// Delete any cached provider that no longer has an associated jar.
11611166
debug!(target: "providers::static_file", ?segment, "Cleaning up jar map");

crates/storage/provider/src/providers/static_file/writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ impl<N: NodePrimitives> StaticFileProviderRW<N> {
723723
prev_path.exists().then_some(prev_block)
724724
});
725725

726-
self.reader().update_index(segment, segment_max_block)
726+
self.reader().update_index(segment, segment_max_block, Some(self.writer.user_header()))
727727
}
728728

729729
/// Ensures that the writer is positioned at the specified block number.

0 commit comments

Comments
 (0)