Skip to content

perf: write and read object data without copying it on the main thread - #3

Merged
AlexanderSlaa merged 1 commit into
SourceRegistry:mainfrom
wido:perf/zero-copy-io
Nov 21, 2026
Merged

AlexanderSlaa merged 1 commit into
SourceRegistry:mainfrom
wido:perf/zero-copy-io

Conversation

@wido

@wido wido commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Every write copies its Buffer into a std::vector on the main thread before it's queued. Every read fills a vector on the thread pool, and the main thread then copies it again into a new Buffer. With objects of several MB that's a big memcpy on the event loop for each operation.

This change:

  • Writes (write, writeFull, append, RbdImage.write) use the Buffer's memory in place and keep a reference to it until the operation is done. As with fs.write(), the caller must not change the Buffer before the promise settles. The README says so.
  • Reads (read, RbdImage.read) read into memory that becomes the returned Buffer. That memory is reported to V8 as external memory, so it gets collected like any other. A read that fills less than half of its allocation is still copied, so a short read doesn't keep the whole allocation alive.

Measured with 64 writes and 64 reads of 4 MiB, 16 at a time, against a Ceph 21 cluster:

main thread busy longest event loop stall
writes 570-626 ms → 57-67 ms 133-157 ms → 2.5-7 ms
reads 339-352 ms → 133-147 ms 31-39 ms → 19-23 ms

Most of the remaining read time is the benchmark itself comparing the bytes.

I also checked that:

  • writes stay intact when the caller drops the Buffer right away and GC is forced while they run;
  • 6.4 GB of reads, with no references kept, grew memory by only 44 MiB;
  • a short read returns only the bytes it read.

@AlexanderSlaa AlexanderSlaa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

Verified correctness:

  • 16 × 2 MiB writes whose Buffers were dropped, with GC forced while they ran, are intact.
  • Buffer slices and pooled small Buffers (non-zero byteOffset) write only their own bytes.
  • Reads that fill exactly half the allocation (zero-copy path) and just under half (copy path) both return the right bytes.
  • Buffers from concurrent reads don't share memory.
  • Reading 1 GiB without keeping references keeps RSS growth bounded.
  • An RBD write/read round trip with GC during the write is intact.

Benchmark (64 × 4 MiB, 16 at a time, arm64, memstore OSD, 3 rounds):

main thread busy longest stall
writes 30–65 ms → 3–4 ms 8–15 ms → 2.2 ms
reads 26–34 ms → 28–35 ms 2.2–3.7 ms → 2.3–3.7 ms

The write improvement reproduces. The read improvement doesn't on this machine. How did you measure reads? It may depend on hardware.

Merge note: this conflicts with #2 in common.h and io_context.cpp. When rebasing, keep the in_flight(state_) calls.

Every write copied its Buffer into a std::vector on the main thread before
queuing the operation, and every read filled a vector that was then copied
into a new Buffer on the main thread. For multi-megabyte objects that is a
large memcpy on the event loop per operation.

Writes (rados_write, rados_write_full, rados_append, rbd_write2) now use the
Buffer's memory in place and keep a reference to it until the worker is
deleted on the main thread. Reads (rados_read, rbd_read2) read into memory
that becomes the returned Buffer, reported to V8 as external memory so it is
collected like any other; a read that uses less than half of its allocation
is still copied, so a short read does not pin the whole allocation.

Like fs.write(), a Buffer passed to a write must not change before the
promise settles. This is documented in the README.

64 writes and 64 reads of 4 MiB, 16 at a time, against a Ceph 21 cluster:

            main thread busy   longest event loop stall
  writes    570-626 ms -> 57-67 ms      133-157 ms -> 2.5-7 ms
  reads     339-352 ms -> 133-147 ms     31-39 ms -> 19-23 ms

(the remaining read time is mostly the benchmark comparing the bytes)
@AlexanderSlaa
AlexanderSlaa merged commit 729de71 into SourceRegistry:main Nov 21, 2026
9 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 21, 2026
## [1.1.2](v1.1.1...v1.1.2) (2026-11-21)

### Performance Improvements

* write and read object data without copying it on the main thread ([#3](#3)) ([729de71](729de71))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.1.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@AlexanderSlaa

Copy link
Copy Markdown
Contributor

Thanks for your contribution! Merged.

I rebased this onto main now that #2 and #6 are in. The conflicts were all side by side:

No changes to your logic. Built with no warnings, and the full suite passes against a Squid cluster, including the GC, slice and short-read checks from my earlier review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants