Skip to content

perf: buffer recycling, pre-allocation, and reusable write buffer - #2337

Merged
oferchen merged 2 commits into
masterfrom
perf/decoupled-receiver
Feb 18, 2026
Merged

oferchen merged 2 commits into
masterfrom
perf/decoupled-receiver

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Buffer recycling: Disk thread returns used Vec<u8> buffers through a return channel; network thread reuses them via try_recv() before allocating new ones, matching upstream rsync's static buffer in simple_recv_token (token.c:284)
  • File pre-allocation: file.set_len(target_size) for direct-write files >= 64KB, mirroring upstream's do_fallocate (receiver.c:254-258)
  • Reusable 256KB write buffer: ReusableBufWriter borrows a thread-local Vec allocated once in disk_thread_main, eliminating per-file BufWriter alloc/free — matches upstream's static wf_writeBuf (fileio.c:161)
  • DiskThreadHandle struct replaces complex tuple return from spawn_disk_thread

Benchmark Results (10K files, 508MB, rsync daemon)

Build Wall time vs Upstream
upstream rsync 3.4.1 895ms baseline
oc-rsync HEAD (decoupled) 764ms 1.17x faster
oc-rsync HEAD2 (this PR) 768ms 1.17x faster

Callgrind: eliminates ~4M allocator instructions (mi_find_page, _mi_page_free_collect, mi_heap_malloc_zero_aligned_at_generic, _mi_malloc_generic all removed from hot path).

Test plan

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings
  • cargo nextest run --workspace --all-features (21,722 tests pass)
  • Hyperfine wall-time benchmark (3-way: upstream vs HEAD vs HEAD2)
  • Callgrind A/B comparison (HEAD vs HEAD2)

Implement a producer-consumer architecture that separates network I/O
from disk I/O onto separate threads, mirroring upstream rsync's fork()
model using Rust threads + bounded channels.

Architecture:
- Network thread: reads delta tokens, resolves block refs, verifies
  checksums, streams chunks through sync_channel(32)
- Disk thread: receives Begin/Chunk/Commit messages, opens files,
  writes data, flushes/renames atomically
- PipelinedReceiver mediator coordinates lifecycle and error propagation
- Drop guard ensures clean shutdown even on panic

New files:
- pipeline/messages.rs: FileMessage, BeginMessage, CommitResult types
- disk_commit.rs: spawn_disk_thread(), DiskCommitConfig
- pipeline/receiver.rs: PipelinedReceiver mediator

Removed:
- pipeline/overlapped.rs: superseded by streaming channel architecture

Benchmark (10K files, 401MB, loopback daemon, aarch64):
- HEAD: 771ms (1.00x baseline)
- v0.5.7: 883ms (1.15x slower)
- upstream rsync 3.4.1: 891ms (1.16x slower)
Mirror upstream rsync's memory management patterns to reduce allocator
overhead in the decoupled receiver's disk commit thread.

- Buffer recycling: disk thread returns used Vec<u8> buffers through a
  return channel; network thread reuses them via try_recv() before
  allocating, matching upstream's static buffer in simple_recv_token
  (token.c:284)

- File pre-allocation: call file.set_len(target_size) for direct-write
  files >= 64KB, mirroring upstream's do_fallocate (receiver.c:254-258)

- Reusable 256KB write buffer: ReusableBufWriter borrows a thread-local
  Vec allocated once in disk_thread_main, eliminating per-file BufWriter
  alloc/free. Matches upstream's static wf_writeBuf (fileio.c:161)

- DiskThreadHandle struct replaces complex tuple return from
  spawn_disk_thread (clippy type_complexity)

Callgrind: eliminates ~4M allocator instructions (mi_find_page,
_mi_page_free_collect, mi_heap_malloc_zero_aligned_at_generic,
_mi_malloc_generic all removed from hot path).

Wall time: 1.17x faster than upstream rsync 3.4.1 (768ms vs 895ms
on 10K files / 508MB via rsync daemon).
@oferchen
oferchen merged commit 6499a97 into master Feb 18, 2026
30 checks passed
@oferchen
oferchen deleted the perf/decoupled-receiver branch February 18, 2026 05:55
oferchen added a commit that referenced this pull request May 1, 2026
)

* perf: decoupled receiver — overlap network reads with disk writes

Implement a producer-consumer architecture that separates network I/O
from disk I/O onto separate threads, mirroring upstream rsync's fork()
model using Rust threads + bounded channels.

Architecture:
- Network thread: reads delta tokens, resolves block refs, verifies
  checksums, streams chunks through sync_channel(32)
- Disk thread: receives Begin/Chunk/Commit messages, opens files,
  writes data, flushes/renames atomically
- PipelinedReceiver mediator coordinates lifecycle and error propagation
- Drop guard ensures clean shutdown even on panic

New files:
- pipeline/messages.rs: FileMessage, BeginMessage, CommitResult types
- disk_commit.rs: spawn_disk_thread(), DiskCommitConfig
- pipeline/receiver.rs: PipelinedReceiver mediator

Removed:
- pipeline/overlapped.rs: superseded by streaming channel architecture

Benchmark (10K files, 401MB, loopback daemon, aarch64):
- HEAD: 771ms (1.00x baseline)
- v0.5.7: 883ms (1.15x slower)
- upstream rsync 3.4.1: 891ms (1.16x slower)

* perf: buffer recycling, pre-allocation, and reusable write buffer

Mirror upstream rsync's memory management patterns to reduce allocator
overhead in the decoupled receiver's disk commit thread.

- Buffer recycling: disk thread returns used Vec<u8> buffers through a
  return channel; network thread reuses them via try_recv() before
  allocating, matching upstream's static buffer in simple_recv_token
  (token.c:284)

- File pre-allocation: call file.set_len(target_size) for direct-write
  files >= 64KB, mirroring upstream's do_fallocate (receiver.c:254-258)

- Reusable 256KB write buffer: ReusableBufWriter borrows a thread-local
  Vec allocated once in disk_thread_main, eliminating per-file BufWriter
  alloc/free. Matches upstream's static wf_writeBuf (fileio.c:161)

- DiskThreadHandle struct replaces complex tuple return from
  spawn_disk_thread (clippy type_complexity)

Callgrind: eliminates ~4M allocator instructions (mi_find_page,
_mi_page_free_collect, mi_heap_malloc_zero_aligned_at_generic,
_mi_malloc_generic all removed from hot path).

Wall time: 1.17x faster than upstream rsync 3.4.1 (768ms vs 895ms
on 10K files / 508MB via rsync daemon).
oferchen added a commit that referenced this pull request May 5, 2026
)

* perf: decoupled receiver — overlap network reads with disk writes

Implement a producer-consumer architecture that separates network I/O
from disk I/O onto separate threads, mirroring upstream rsync's fork()
model using Rust threads + bounded channels.

Architecture:
- Network thread: reads delta tokens, resolves block refs, verifies
  checksums, streams chunks through sync_channel(32)
- Disk thread: receives Begin/Chunk/Commit messages, opens files,
  writes data, flushes/renames atomically
- PipelinedReceiver mediator coordinates lifecycle and error propagation
- Drop guard ensures clean shutdown even on panic

New files:
- pipeline/messages.rs: FileMessage, BeginMessage, CommitResult types
- disk_commit.rs: spawn_disk_thread(), DiskCommitConfig
- pipeline/receiver.rs: PipelinedReceiver mediator

Removed:
- pipeline/overlapped.rs: superseded by streaming channel architecture

Benchmark (10K files, 401MB, loopback daemon, aarch64):
- HEAD: 771ms (1.00x baseline)
- v0.5.7: 883ms (1.15x slower)
- upstream rsync 3.4.1: 891ms (1.16x slower)

* perf: buffer recycling, pre-allocation, and reusable write buffer

Mirror upstream rsync's memory management patterns to reduce allocator
overhead in the decoupled receiver's disk commit thread.

- Buffer recycling: disk thread returns used Vec<u8> buffers through a
  return channel; network thread reuses them via try_recv() before
  allocating, matching upstream's static buffer in simple_recv_token
  (token.c:284)

- File pre-allocation: call file.set_len(target_size) for direct-write
  files >= 64KB, mirroring upstream's do_fallocate (receiver.c:254-258)

- Reusable 256KB write buffer: ReusableBufWriter borrows a thread-local
  Vec allocated once in disk_thread_main, eliminating per-file BufWriter
  alloc/free. Matches upstream's static wf_writeBuf (fileio.c:161)

- DiskThreadHandle struct replaces complex tuple return from
  spawn_disk_thread (clippy type_complexity)

Callgrind: eliminates ~4M allocator instructions (mi_find_page,
_mi_page_free_collect, mi_heap_malloc_zero_aligned_at_generic,
_mi_malloc_generic all removed from hot path).

Wall time: 1.17x faster than upstream rsync 3.4.1 (768ms vs 895ms
on 10K files / 508MB via rsync daemon).
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