Skip to content

Commit e442ff4

Browse files
spikehmeta-codesync[bot]
authored andcommitted
Use IOBufPtr for binary payload fields
Summary: The bare "binary" type in Thrift IDL maps to std::string in the generated C++ code. On the deserialization path this hits the std::string overload of apache::thrift::detail::readStringBody (Protocol.h:435), which calls folly::io::Cursor::pullAtMost into the string's storage - a memcpy of the full payload out of the receive IOBuf chain. Switch every bare binary payload field in StressTest.thrift to the existing IOBufPtr typedef, which is annotated with `cpp.Type{name = "std::unique_ptr<folly::IOBuf>"}`. The generated reader then dispatches to the IOBuf overload of readBinary, which clones buffer references via Cursor::clone - refcount bumps, no payload memcpy. This is a wire-compatible change (still serialized as a binary field) but is a source-incompatible C++ API change: handlers/clients now see `std::unique_ptr<folly::IOBuf>` instead of std::string. The stoptls_payload fields already used IOBufPtr, so handler code already knows how to consume the IOBuf form. Differential Revision: D101410011 fbshipit-source-id: 94c337e83b83584acbee37fc31f875276058e62e
1 parent b665d88 commit e442ff4

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

thrift/conformance/stresstest/if/StressTest.thrift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ struct StorageReadRequest {
9595
}
9696

9797
struct StorageReadResponse {
98-
1: binary payload;
98+
1: IOBufPtr payload;
9999
}
100100

101101
struct StorageWriteRequest {
102-
1: binary payload;
102+
1: IOBufPtr payload;
103103
}
104104

105105
struct StorageWriteResponse {}

thrift/conformance/stresstest/server/StressTestHandler.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ void StressTestHandler::async_tm_storageReadTm(
305305
auto response = std::make_unique<StorageReadResponse>();
306306
auto size = *request->responseBytes();
307307
if (size > 0) {
308-
response->payload() = std::string(size, 'x');
308+
auto buf = folly::IOBuf::create(size);
309+
std::memset(buf->writableData(), 'x', size);
310+
buf->append(size);
311+
response->payload() = std::move(buf);
309312
}
310313
callback->result(std::move(response));
311314
}
@@ -316,7 +319,10 @@ void StressTestHandler::async_eb_storageReadEb(
316319
auto response = std::make_unique<StorageReadResponse>();
317320
auto size = *request->responseBytes();
318321
if (size > 0) {
319-
response->payload() = std::string(size, 'x');
322+
auto buf = folly::IOBuf::create(size);
323+
std::memset(buf->writableData(), 'x', size);
324+
buf->append(size);
325+
response->payload() = std::move(buf);
320326
}
321327
callback->result(std::move(response));
322328
}

0 commit comments

Comments
 (0)