Add IOBuf<->std::iostream adapters for zero-copy I/O#51
Open
chenBright wants to merge 1 commit into
Open
Conversation
0a48a11 to
3028871
Compare
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
3028871 to
d2f086a
Compare
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: