Commit c06378a2 authored by Michael Voznesensky's avatar Michael Voznesensky Committed by Facebook GitHub Bot

Make IOBuf construct from Stringpiece instead of std::string

Summary: Title

Reviewed By: yfeldblum

Differential Revision: D32573163

fbshipit-source-id: c34c4de9795dcb2796045f5c464757b8f3d544d9
parent a1e3a3cf
......@@ -537,24 +537,19 @@ class IOBuf {
* copyBuffer() above, with the size argument of 3.
*/
static std::unique_ptr<IOBuf> copyBuffer(
const std::string& buf,
std::size_t headroom = 0,
std::size_t minTailroom = 0);
StringPiece buf, std::size_t headroom = 0, std::size_t minTailroom = 0);
IOBuf(
CopyBufferOp op,
const std::string& buf,
StringPiece buf,
std::size_t headroom = 0,
std::size_t minTailroom = 0)
: IOBuf(op, buf.data(), buf.size(), headroom, minTailroom) {}
/**
* A version of copyBuffer() that returns a null pointer if the input string
* is empty.
*/
static std::unique_ptr<IOBuf> maybeCopyBuffer(
const std::string& buf,
std::size_t headroom = 0,
std::size_t minTailroom = 0);
StringPiece buf, std::size_t headroom = 0, std::size_t minTailroom = 0);
/**
* Convenience function to free a chain of IOBufs held by a unique_ptr.
......@@ -1780,12 +1775,12 @@ inline std::unique_ptr<IOBuf> IOBuf::copyBuffer(
}
inline std::unique_ptr<IOBuf> IOBuf::copyBuffer(
const std::string& buf, std::size_t headroom, std::size_t minTailroom) {
StringPiece buf, std::size_t headroom, std::size_t minTailroom) {
return copyBuffer(buf.data(), buf.size(), headroom, minTailroom);
}
inline std::unique_ptr<IOBuf> IOBuf::maybeCopyBuffer(
const std::string& buf, std::size_t headroom, std::size_t minTailroom) {
StringPiece buf, std::size_t headroom, std::size_t minTailroom) {
if (buf.empty()) {
return nullptr;
}
......
......@@ -77,6 +77,22 @@ BENCHMARK(copyBenchmark, iters) {
}
}
BENCHMARK(copyBufferFromStringBenchmark, iters) {
std::string s("Hello World");
while (iters--) {
auto copy = IOBuf::copyBuffer(s);
folly::doNotOptimizeAway(copy->capacity());
}
}
BENCHMARK(copyBufferFromStringPieceBenchmark, iters) {
folly::StringPiece s("Hello World");
while (iters--) {
auto copy = IOBuf::copyBuffer(s);
folly::doNotOptimizeAway(copy->capacity());
}
}
BENCHMARK(cloneCoalescedBaseline, iters) {
std::unique_ptr<IOBuf> buf = IOBuf::createChain(100, 10);
while (iters--) {
......
......@@ -1808,3 +1808,21 @@ TEST(IOBuf, AppendTo) {
testAppendTo(std::vector<char>{});
testAppendTo(std::vector<unsigned char>{});
}
TEST(IOBuf, FromStringView) {
auto literalHelloBuffer = IOBuf::copyBuffer("Hello");
std::string hello("Hello");
auto fromStringHelloBuffer = IOBuf::copyBuffer(hello);
std::string_view hello2("Hello");
auto fromStringViewHelloBuffer = IOBuf::copyBuffer(hello2);
std::string fromLiteral;
literalHelloBuffer->appendTo(fromLiteral);
std::string fromString;
fromStringHelloBuffer->appendTo(fromString);
std::string fromStringView;
fromStringViewHelloBuffer->appendTo(fromStringView);
EXPECT_EQ(fromStringView, fromString);
EXPECT_EQ(fromLiteral, fromString);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment