Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
perf(reconstruction): start streamed downloads with a 2MB block
A stream returns no data until its first term is downloaded, and that
term can span the whole first block of reconstruction terms. The first
block was min_reconstruction_fetch_size (256MB) and the first term of a
file is often a whole 67MB xorb, so the first byte of a stream waited for
that download (9 to 27s on a laptop when HfFileSystem streams through
hf_xet).

Streams now queue their two initial blocks at stream_initial_fetch_size
(2MB, capped at min_reconstruction_fetch_size) and twice that size; the
prefetcher sizes later blocks as before. File downloads keep their initial
blocks, and bounded ranges smaller than min_reconstruction_fetch_size
still go in one reconstruction call.
  • Loading branch information
XciD committed Oct 26, 2026
commit 84ac206aa0f1494e93fca008db4ab5c03cb50268
15 changes: 15 additions & 0 deletions api_changes/update_261026_stream_initial_fetch_size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
This update makes streamed reconstructions start with small term blocks, so the first bytes of a stream arrive sooner.

What changed
- Added the config value `reconstruction.stream_initial_fetch_size` (default 2MB, environment variable `HF_XET_RECONSTRUCTION_STREAM_INITIAL_FETCH_SIZE`).
- `ReconstructionTermManager::new` takes a new last argument, `is_streaming: bool`.
- When true, the two initial blocks are `stream_initial_fetch_size` and twice that size, capped at `min_reconstruction_fetch_size`.
- When false, the initial blocks are unchanged (`min_reconstruction_fetch_size` and twice that size).
- `FileReconstructor::run` passes `is_streaming` through, so `reconstruct_to_stream` and `reconstruct_to_unordered_stream` (and the wasm `reconstruct_to_writer`, which streams) use the small initial blocks.

Why this matters
- A stream yields nothing until its first term is downloaded, and that term can span the whole first block (up to `min_reconstruction_fetch_size`, 256MB by default). With a 67MB first term, a laptop benchmark waited 9 to 27s for the first byte.

Usage notes
- Callers of `ReconstructionTermManager::new` must pass `is_streaming`; pass `false` to keep the previous behavior.
- Bounded ranges smaller than about `min_reconstruction_fetch_size` still go in one block: the first block extends to the end of the range when the remainder is below `min_reconstruction_fetch_size`.
38 changes: 33 additions & 5 deletions xet_data/src/file_reconstruction/file_reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ impl FileReconstructor {
/// Runs the file reconstruction with error handling and cancellation support.
/// Returns the number of bytes written.
///
/// When `is_streaming` is true, the progress completion assertions at the end
/// of reconstruction are skipped because the stream consumer reports bytes
/// asynchronously after this method returns.
/// When `is_streaming` is true, the first term blocks use `stream_initial_fetch_size`,
/// and the progress completion assertions at the end of reconstruction are skipped
/// because the stream consumer reports bytes asynchronously after this method returns.
pub(crate) async fn run(
self,
data_writer: Box<dyn DataWriter>,
Expand All @@ -262,7 +262,7 @@ impl FileReconstructor {
self,
mut data_writer: Box<dyn DataWriter>,
run_state: &RunState,
_is_streaming: bool,
is_streaming: bool,
) -> std::result::Result<u64, RunError> {
let Self {
ctx,
Expand All @@ -286,6 +286,7 @@ impl FileReconstructor {
file_hash,
requested_range,
run_state.progress_updater().cloned(),
is_streaming,
)
.await?;

Expand Down Expand Up @@ -428,7 +429,7 @@ impl FileReconstructor {
run_state.log_progress("File reconstruction completed successfully");

#[cfg(debug_assertions)]
if !_is_streaming && let Some(updater) = run_state.progress_updater() {
if !is_streaming && let Some(updater) = run_state.progress_updater() {
updater.assert_complete();
if let Some(byte_range) = byte_range
&& byte_range.end < u64::MAX
Expand Down Expand Up @@ -722,6 +723,33 @@ mod tests {
reconstruct_and_verify_full(&client, &file_contents, test_config()).await;
}

#[tokio::test]
async fn test_stream_first_chunk_bounded_by_stream_initial_fetch_size() {
// A single term of 64 chunks: the first streamed chunk is the part of it covered by the first block.
let (client, file_contents) = setup_test_file(&[(1, (0, 64))]).await;

let mut config = ReconstructionConfig::default();
config.min_reconstruction_fetch_size = xet_runtime::utils::ByteSize::from("1mb");
config.stream_initial_fetch_size = xet_runtime::utils::ByteSize::from("500");

let mut stream = FileReconstructor::new(
&XetContext::default().unwrap(),
&(client as Arc<dyn Client>),
file_contents.file_hash,
)
.with_config(config)
.reconstruct_to_stream();

let first_chunk = stream.next().await.unwrap().unwrap();
assert!(first_chunk.len() <= 500 + TEST_CHUNK_SIZE, "first chunk is {} bytes", first_chunk.len());

let mut data = first_chunk.to_vec();
while let Some(chunk) = stream.next().await.unwrap() {
data.extend_from_slice(&chunk);
}
assert_eq!(data, file_contents.data);
}

// ==================== Progress tracker tests ====================

#[tokio::test]
Expand Down
17 changes: 12 additions & 5 deletions xet_data/src/file_reconstruction/reconstruction_terms/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl ReconstructionTermManager {
file_hash: MerkleHash,
file_byte_range: FileRange,
progress_updater: Option<Arc<ItemProgressUpdater>>,
is_streaming: bool,
) -> Result<Self> {
let completion_rate_estimator =
ExpWeightedMovingAvg::new_count_decay(config.completion_rate_estimator_half_life);
Expand Down Expand Up @@ -80,7 +81,15 @@ impl ReconstructionTermManager {
// once the first block is finished, we have a second block to start processing -- and
// an estimate of the completion time based on the first one. This helps us to get
// a better estimate of the completion time.
let initial_fetch_size = s.config.min_reconstruction_fetch_size.as_u64();
//
// A stream returns no data until its first term is downloaded, and that term can span the
// whole first block, so a stream starts with smaller blocks to return its first bytes sooner.
let min_fetch_size = s.config.min_reconstruction_fetch_size.as_u64();
let initial_fetch_size = if is_streaming {
s.config.stream_initial_fetch_size.as_u64().min(min_fetch_size)
} else {
min_fetch_size
};
s.prefetch_block(initial_fetch_size).await?;
s.prefetch_block(2 * initial_fetch_size).await?;

Expand Down Expand Up @@ -237,10 +246,8 @@ impl ReconstructionTermManager {
}

async fn prefetch_block(&mut self, block_size: u64) -> Result<()> {
let block_size = block_size.clamp(
self.config.min_reconstruction_fetch_size.as_u64(),
self.config.max_reconstruction_fetch_size.as_u64(),
);
// No lower bound here: the initial blocks of a stream are smaller than `min_reconstruction_fetch_size`.
let block_size = block_size.min(self.config.max_reconstruction_fetch_size.as_u64());

// First, check the block range to see if we're over the requested range.
let mut prefetch_block_range =
Expand Down
10 changes: 10 additions & 0 deletions xet_runtime/src/config/groups/reconstruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ crate::config_group!({
/// Use the environment variable `HF_XET_RECONSTRUCTION_MAX_RECONSTRUCTION_FETCH_SIZE` to set this value.
ref max_reconstruction_fetch_size: ByteSize = ByteSize::from("8gb");

/// The size of the first fetch request of a streamed reconstruction; the second one is twice this size.
/// A stream returns no data until its first term is downloaded, and that term can span the whole first
/// fetch, so a small first fetch keeps the time to first byte short. Later fetches follow the sizes above.
/// This value is capped at `min_reconstruction_fetch_size`.
///
/// The default value is 2MB.
///
/// Use the environment variable `HF_XET_RECONSTRUCTION_STREAM_INITIAL_FETCH_SIZE` to set this value.
ref stream_initial_fetch_size: ByteSize = ByteSize::from("2mb");

/// The amount of download buffer always available for file reconstruction.
/// The full buffer size will be this plus the number of simultaneous active
/// file downloads times the per file size up to the global limit of
Expand Down
Loading