Skip to content

perf: reduce checksum and memcpy overhead in receiver pipeline - #2340

Merged
oferchen merged 5 commits into
masterfrom
perf/reduce-checksum-memcpy-overhead
Feb 18, 2026
Merged

oferchen merged 5 commits into
masterfrom
perf/reduce-checksum-memcpy-overhead

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Move per-file checksum computation from network thread to disk commit thread, overlapping hashing (42% of instructions) with disk I/O. The network thread now focuses solely on reading the wire protocol.
  • Remove redundant BufReader from the input I/O path — MultiplexReader provides its own 64KB buffer, eliminating one memcpy layer per byte received.
  • Bypass write buffer for large chunks (≥ 8KB) in the disk commit thread, writing directly to the file descriptor.
  • Read cross-frame tokens directly into channel buffer, eliminating the intermediate TokenBuffer copy.

Architecture

BeginMessage now carries an Option<ChecksumVerifier> to the disk thread. The disk thread hashes each chunk as it writes and returns a ComputedChecksum in CommitResult. PipelinedReceiver maintains a FIFO queue of expected checksums and verifies them when draining results.

Benchmark

10,000 files (422 MB), daemon loopback, --no-whole-file disabled:

Version Time (mean) vs upstream
upstream rsync 3.4.1 892 ms 1.00x
oc-rsync baseline (master) 743 ms 1.19x
oc-rsync HEAD 604 ms 1.50x

Data integrity verified via md5sum comparison of all 10,000 files.

Test plan

  • cargo clippy --workspace --all-targets --all-features --no-deps -D warnings
  • cargo nextest run --workspace --all-features (21,724 tests pass)
  • Container benchmark: 1.50x faster than upstream
  • Data integrity: md5sum match on 10,000 files

Add a DIRECT_WRITE_THRESHOLD (8 KB) to ReusableBufWriter. Chunks at or
above this size skip the intermediate buffer copy and write directly to
the file descriptor. This eliminates one memcpy from the hot path for
typical rsync literal tokens (32 KB+) while preserving syscall batching
for tiny delta tokens.

The 256 KB write buffer (matching upstream's wf_writeBuf) is still used
for small chunks to amortize syscall overhead.
…read

Defer per-file integrity checksum (XXH3/MD5/etc) from the network thread
to the disk commit thread, overlapping hashing with disk I/O. The network
thread now focuses solely on reading the wire protocol.

Architecture:
- BeginMessage carries Option<ChecksumVerifier> to the disk thread
- Disk thread calls verifier.update() on each chunk before writing
- CommitResult returns ComputedChecksum for deferred verification
- PipelinedReceiver maintains a FIFO queue of expected checksums
  and verifies each CommitResult in drain_ready/drain_all_results

Benchmark: 1.50x faster than upstream rsync (604ms vs 909ms, 10K files,
422MB, data integrity verified via md5sum comparison).
Remove the 64KB BufReader that wrapped stdin before MultiplexReader.
MultiplexReader already provides its own 64KB buffer (matching upstream
rsync's iobuf_in), making the outer BufReader redundant. Data now flows
directly from the socket to MultiplexReader's buffer, eliminating one
memcpy layer on every byte received.

This mirrors the write side, which already documents: "No BufWriter:
MultiplexWriter provides all buffering."
In the streaming token loop, the fallback path for tokens spanning
multiplex frame boundaries previously copied data through TokenBuffer
before copying again into the channel Vec. Now reads directly into
the Vec, eliminating one memcpy for cross-frame tokens.

Also removes the now-unused token_buffer parameter from
process_file_response_streaming and its callers.
@oferchen
oferchen merged commit e41d8d3 into master Feb 18, 2026
29 of 30 checks passed
@oferchen
oferchen deleted the perf/reduce-checksum-memcpy-overhead branch February 18, 2026 17:30
oferchen added a commit that referenced this pull request May 1, 2026
…erhead

perf: reduce checksum and memcpy overhead in receiver pipeline
oferchen added a commit that referenced this pull request May 5, 2026
…erhead

perf: reduce checksum and memcpy overhead in receiver pipeline
oferchen added a commit that referenced this pull request May 18, 2026
…_bytes (STN-6 #2340)

Adds an opt-in process-RSS threshold that forces the concurrent-delta
reorder buffer to spill independently of the byte budget. When set,
`SpillableReorderBuffer` consults a cached `/proc/self/statm` reading
(refreshed every 100 ms) before checking `threshold_bytes`, so transient
RSS spikes drain at least one item to disk per insert. Default behaviour
is preserved: the new field defaults to `None`, all existing tests pass
without modification.

Platform handling:
- Linux reads `/proc/self/statm` and multiplies the resident-pages field
  by the safe `rustix::param::page_size()` shim.
- macOS ships an `Ok(0)` stub so the knob is a no-op until a follow-up
  wires `mach_task_basic_info` (tracked separately).
- Other platforms (notably Windows) return `Unsupported`; the buffer
  silently falls back to the byte-budget path.

The RSS module sits under `crates/engine/src/concurrent_delta/spill/rss.rs`
with no unsafe code in the engine crate (rustix already covers the page
size lookup) and three unit tests cover the default, threshold-trip, and
unsupported-platform paths. `SpillPolicy.memory_pressure_bytes` is the
last field in the struct so STN-3/4/5 rebases stay mechanical with
`..Default::default()`.
oferchen added a commit that referenced this pull request May 18, 2026
…_bytes (STN-6 #2340)

Adds an opt-in process-RSS threshold that forces the concurrent-delta
reorder buffer to spill independently of the byte budget. When set,
`SpillableReorderBuffer` consults a cached `/proc/self/statm` reading
(refreshed every 100 ms) before checking `threshold_bytes`, so transient
RSS spikes drain at least one item to disk per insert. Default behaviour
is preserved: the new field defaults to `None`, all existing tests pass
without modification.

Platform handling:
- Linux reads `/proc/self/statm` and multiplies the resident-pages field
  by the safe `rustix::param::page_size()` shim.
- macOS ships an `Ok(0)` stub so the knob is a no-op until a follow-up
  wires `mach_task_basic_info` (tracked separately).
- Other platforms (notably Windows) return `Unsupported`; the buffer
  silently falls back to the byte-budget path.

The RSS module sits under `crates/engine/src/concurrent_delta/spill/rss.rs`
with no unsafe code in the engine crate (rustix already covers the page
size lookup) and three unit tests cover the default, threshold-trip, and
unsupported-platform paths. `SpillPolicy.memory_pressure_bytes` is the
last field in the struct so STN-3/4/5 rebases stay mechanical with
`..Default::default()`.
oferchen added a commit that referenced this pull request May 18, 2026
…_bytes (STN-6 #2340)

Adds an opt-in process-RSS threshold that forces the concurrent-delta
reorder buffer to spill independently of the byte budget. When set,
`SpillableReorderBuffer` consults a cached `/proc/self/statm` reading
(refreshed every 100 ms) before checking `threshold_bytes`, so transient
RSS spikes drain at least one item to disk per insert. Default behaviour
is preserved: the new field defaults to `None`, all existing tests pass
without modification.

Platform handling:
- Linux reads `/proc/self/statm` and multiplies the resident-pages field
  by the safe `rustix::param::page_size()` shim.
- macOS ships an `Ok(0)` stub so the knob is a no-op until a follow-up
  wires `mach_task_basic_info` (tracked separately).
- Other platforms (notably Windows) return `Unsupported`; the buffer
  silently falls back to the byte-budget path.

The RSS module sits under `crates/engine/src/concurrent_delta/spill/rss.rs`
with no unsafe code in the engine crate (rustix already covers the page
size lookup) and three unit tests cover the default, threshold-trip, and
unsupported-platform paths. `SpillPolicy.memory_pressure_bytes` is the
last field in the struct so STN-3/4/5 rebases stay mechanical with
`..Default::default()`.

# Conflicts:
#	crates/engine/src/concurrent_delta/consumer.rs
#	crates/engine/src/concurrent_delta/spill/mod.rs
#	crates/engine/src/concurrent_delta/spill/policy.rs
oferchen added a commit that referenced this pull request May 18, 2026
…_bytes (STN-6 #2340) (#4421)

Adds an opt-in process-RSS threshold that forces the concurrent-delta
reorder buffer to spill independently of the byte budget. When set,
`SpillableReorderBuffer` consults a cached `/proc/self/statm` reading
(refreshed every 100 ms) before checking `threshold_bytes`, so transient
RSS spikes drain at least one item to disk per insert. Default behaviour
is preserved: the new field defaults to `None`, all existing tests pass
without modification.

Platform handling:
- Linux reads `/proc/self/statm` and multiplies the resident-pages field
  by the safe `rustix::param::page_size()` shim.
- macOS ships an `Ok(0)` stub so the knob is a no-op until a follow-up
  wires `mach_task_basic_info` (tracked separately).
- Other platforms (notably Windows) return `Unsupported`; the buffer
  silently falls back to the byte-budget path.

The RSS module sits under `crates/engine/src/concurrent_delta/spill/rss.rs`
with no unsafe code in the engine crate (rustix already covers the page
size lookup) and three unit tests cover the default, threshold-trip, and
unsupported-platform paths. `SpillPolicy.memory_pressure_bytes` is the
last field in the struct so STN-3/4/5 rebases stay mechanical with
`..Default::default()`.

# Conflicts:
#	crates/engine/src/concurrent_delta/consumer.rs
#	crates/engine/src/concurrent_delta/spill/mod.rs
#	crates/engine/src/concurrent_delta/spill/policy.rs
oferchen added a commit that referenced this pull request May 18, 2026
…_bytes (STN-6 #2340) (#4421)

Adds an opt-in process-RSS threshold that forces the concurrent-delta
reorder buffer to spill independently of the byte budget. When set,
`SpillableReorderBuffer` consults a cached `/proc/self/statm` reading
(refreshed every 100 ms) before checking `threshold_bytes`, so transient
RSS spikes drain at least one item to disk per insert. Default behaviour
is preserved: the new field defaults to `None`, all existing tests pass
without modification.

Platform handling:
- Linux reads `/proc/self/statm` and multiplies the resident-pages field
  by the safe `rustix::param::page_size()` shim.
- macOS ships an `Ok(0)` stub so the knob is a no-op until a follow-up
  wires `mach_task_basic_info` (tracked separately).
- Other platforms (notably Windows) return `Unsupported`; the buffer
  silently falls back to the byte-budget path.

The RSS module sits under `crates/engine/src/concurrent_delta/spill/rss.rs`
with no unsafe code in the engine crate (rustix already covers the page
size lookup) and three unit tests cover the default, threshold-trip, and
unsupported-platform paths. `SpillPolicy.memory_pressure_bytes` is the
last field in the struct so STN-3/4/5 rebases stay mechanical with
`..Default::default()`.

# Conflicts:
#	crates/engine/src/concurrent_delta/consumer.rs
#	crates/engine/src/concurrent_delta/spill/mod.rs
#	crates/engine/src/concurrent_delta/spill/policy.rs
oferchen added a commit that referenced this pull request May 18, 2026
…_bytes (STN-6 #2340) (#4421)

Adds an opt-in process-RSS threshold that forces the concurrent-delta
reorder buffer to spill independently of the byte budget. When set,
`SpillableReorderBuffer` consults a cached `/proc/self/statm` reading
(refreshed every 100 ms) before checking `threshold_bytes`, so transient
RSS spikes drain at least one item to disk per insert. Default behaviour
is preserved: the new field defaults to `None`, all existing tests pass
without modification.

Platform handling:
- Linux reads `/proc/self/statm` and multiplies the resident-pages field
  by the safe `rustix::param::page_size()` shim.
- macOS ships an `Ok(0)` stub so the knob is a no-op until a follow-up
  wires `mach_task_basic_info` (tracked separately).
- Other platforms (notably Windows) return `Unsupported`; the buffer
  silently falls back to the byte-budget path.

The RSS module sits under `crates/engine/src/concurrent_delta/spill/rss.rs`
with no unsafe code in the engine crate (rustix already covers the page
size lookup) and three unit tests cover the default, threshold-trip, and
unsupported-platform paths. `SpillPolicy.memory_pressure_bytes` is the
last field in the struct so STN-3/4/5 rebases stay mechanical with
`..Default::default()`.

# Conflicts:
#	crates/engine/src/concurrent_delta/consumer.rs
#	crates/engine/src/concurrent_delta/spill/mod.rs
#	crates/engine/src/concurrent_delta/spill/policy.rs
oferchen added a commit that referenced this pull request Aug 24, 2026
…_bytes (STN-6 #2340) (#4421)

Adds an opt-in process-RSS threshold that forces the concurrent-delta
reorder buffer to spill independently of the byte budget. When set,
`SpillableReorderBuffer` consults a cached `/proc/self/statm` reading
(refreshed every 100 ms) before checking `threshold_bytes`, so transient
RSS spikes drain at least one item to disk per insert. Default behaviour
is preserved: the new field defaults to `None`, all existing tests pass
without modification.

Platform handling:
- Linux reads `/proc/self/statm` and multiplies the resident-pages field
  by the safe `rustix::param::page_size()` shim.
- macOS ships an `Ok(0)` stub so the knob is a no-op until a follow-up
  wires `mach_task_basic_info` (tracked separately).
- Other platforms (notably Windows) return `Unsupported`; the buffer
  silently falls back to the byte-budget path.

The RSS module sits under `crates/engine/src/concurrent_delta/spill/rss.rs`
with no unsafe code in the engine crate (rustix already covers the page
size lookup) and three unit tests cover the default, threshold-trip, and
unsupported-platform paths. `SpillPolicy.memory_pressure_bytes` is the
last field in the struct so STN-3/4/5 rebases stay mechanical with
`..Default::default()`.

# Conflicts:
#	crates/engine/src/concurrent_delta/consumer.rs
#	crates/engine/src/concurrent_delta/spill/mod.rs
#	crates/engine/src/concurrent_delta/spill/policy.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant