Add IOBuf<->std::iostream adapters for zero-copy I/O#3341
Merged
Conversation
Introduce four classes that bridge IOBuf and the standard iostream
hierarchy, enabling parsers and serializers that take std::istream& /
std::ostream& (e.g. nlohmann::json) to read from and write to IOBuf
directly without an intermediate string copy:
- IOBufAsInputStreamBuf : read-only streambuf over IOBuf blocks
- IOBufInputStream : std::istream view over IOBuf
- IOBufAsOutputStreamBuf : append-only streambuf, reusing
IOBufAsZeroCopyOutputStream's TLS block pool
- IOBufOutputStream : std::ostream view over IOBuf
Contributor
|
LGTM |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds zero-copy adapters between butil::IOBuf and the standard std::istream/std::ostream ecosystem by introducing std::streambuf-based wrappers, enabling direct integration with libraries like nlohmann::json without converting through std::string.
Changes:
- Add
IOBufAs{Input,Output}StreamBufplusIOBuf{Input,Output}Streamwrappers insrc/butil/iobuf.{h,cpp}. - Add unit tests validating correctness across multi-block buffers and (optionally) round-tripping with
nlohmann::json. - Add Bazel dependencies for
nlohmann_jsonvia bothWORKSPACE(http_archive) andMODULE.bazel(bzlmod), and wire it into the test target.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| WORKSPACE | Adds http_archive for nlohmann_json to support new adapter tests. |
| MODULE.bazel | Adds nlohmann_json as a Bazel module dev dependency for tests. |
| test/BUILD.bazel | Enables JSON-backed tests and links @nlohmann_json//:json into butil_unittests. |
| test/iobuf_unittest.cpp | Adds extensive tests for the new iostream adapters and JSON round-trip coverage. |
| src/butil/iobuf.h | Declares the new streambuf and iostream adapter classes with usage documentation. |
| src/butil/iobuf.cpp | Implements the new streambuf adapters (underflow/xsgetn/showmanyc and overflow/xsputn/sync). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: resolve
Problem Summary:
IOBufintegrates with protobuf viaIOBufAsZeroCopy{Input,Output}Stream, butthere is no direct bridge to the
std::iostreamhierarchy. As a result, anylibrary that consumes/produces
std::istream&/std::ostream&— most notablynlohmann::json— cannot read from or write to anIOBufwithout anintermediate
std::string:This is an obstacle to adopting nlohmann::json in bRPC.
What is changed and the side effects?
Changed:
Introduce four classes in
src/butil/iobuf.{h,cpp}that bridge IOBuf andthe standard iostream hierarchy:
IOBufAsInputStreamBuf— read-onlystd::streambufover an IOBuf,walking the backing blocks via the public
backing_block()API. Overridesunderflow,xsgetn(bulk memcpy across block boundaries to avoid thedefault per-byte sbumpc loop), and
showmanyc(saturating sum ofremaining block sizes).
IOBufInputStream—std::istreamview that wires the streambuf above.IOBufAsOutputStreamBuf— append-onlystd::streambufthat delegates tothe existing
IOBufAsZeroCopyOutputStreamfor block allocation and thethree-branch
BackUp(including the shared-block degradation that protectsIOBufs sharing blocks from being corrupted by an in-flight write). Overrides
overflow,xsputn(block-sized memcpy instead of per-byte sputc),and
sync(forwards to shrink()).IOBufOutputStream—std::ostreamview that wires the streambuf above;accepts an optional block_size to allocate dedicated blocks instead of
drawing from the per-thread TLS pool.
Usage with nlohmann::json:
Side effects:
Performance effects:
Breaking backward compatibility:
Check List: