Repository navigation
perf: write and read object data without copying it on the main thread - #3
Conversation
AlexanderSlaa
left a comment
There was a problem hiding this comment.
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)
cdfb880 to
8d2976d
Compare
## [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))
|
🎉 This PR is included in version 1.1.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
Thanks for your contribution! Merged. I rebased this onto
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. |
Every write copies its Buffer into a
std::vectoron 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:
write,writeFull,append,RbdImage.write) use the Buffer's memory in place and keep a reference to it until the operation is done. As withfs.write(), the caller must not change the Buffer before the promise settles. The README says so.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:
Most of the remaining read time is the benchmark itself comparing the bytes.
I also checked that: