Commit b8860675 authored by Uddipta Maity's avatar Uddipta Maity Committed by Facebook Github Bot

Adding support for IOBuf coalesce with certain headroom and tailroom

Summary:
We need this in wormhole, as the capacity of the IOBuf may be very large, but,
the data length is much smaller. Current implementation of coalesce() allocates
large amount of memory unnecessarily.
Adding methods to be able to set headroom and tailroom during coalesce.

Reviewed By: yfeldblum

Differential Revision: D10386802

fbshipit-source-id: a0d787f41ed195eef409b942aaad583fc22692a3
parent afd02e08
...@@ -536,6 +536,13 @@ unique_ptr<IOBuf> IOBuf::cloneCoalesced() const { ...@@ -536,6 +536,13 @@ unique_ptr<IOBuf> IOBuf::cloneCoalesced() const {
return std::make_unique<IOBuf>(cloneCoalescedAsValue()); return std::make_unique<IOBuf>(cloneCoalescedAsValue());
} }
unique_ptr<IOBuf> IOBuf::cloneCoalescedWithHeadroomTailroom(
std::size_t newHeadroom,
std::size_t newTailroom) const {
return std::make_unique<IOBuf>(
cloneCoalescedAsValueWithHeadroomTailroom(newHeadroom, newTailroom));
}
IOBuf IOBuf::cloneAsValue() const { IOBuf IOBuf::cloneAsValue() const {
auto tmp = cloneOneAsValue(); auto tmp = cloneOneAsValue();
...@@ -561,13 +568,19 @@ IOBuf IOBuf::cloneOneAsValue() const { ...@@ -561,13 +568,19 @@ IOBuf IOBuf::cloneOneAsValue() const {
} }
IOBuf IOBuf::cloneCoalescedAsValue() const { IOBuf IOBuf::cloneCoalescedAsValue() const {
const std::size_t newHeadroom = headroom();
const std::size_t newTailroom = prev()->tailroom();
return cloneCoalescedAsValueWithHeadroomTailroom(newHeadroom, newTailroom);
}
IOBuf IOBuf::cloneCoalescedAsValueWithHeadroomTailroom(
std::size_t newHeadroom,
std::size_t newTailroom) const {
if (!isChained()) { if (!isChained()) {
return cloneOneAsValue(); return cloneOneAsValue();
} }
// Coalesce into newBuf // Coalesce into newBuf
const std::size_t newLength = computeChainDataLength(); const std::size_t newLength = computeChainDataLength();
const std::size_t newHeadroom = headroom();
const std::size_t newTailroom = prev()->tailroom();
const std::size_t newCapacity = newLength + newHeadroom + newTailroom; const std::size_t newCapacity = newLength + newHeadroom + newTailroom;
IOBuf newBuf{CREATE, newCapacity}; IOBuf newBuf{CREATE, newCapacity};
newBuf.advance(newHeadroom); newBuf.advance(newHeadroom);
......
...@@ -1093,8 +1093,23 @@ class IOBuf { ...@@ -1093,8 +1093,23 @@ class IOBuf {
* Returns ByteRange that points to the data IOBuf stores. * Returns ByteRange that points to the data IOBuf stores.
*/ */
ByteRange coalesce() { ByteRange coalesce() {
const std::size_t newHeadroom = headroom();
const std::size_t newTailroom = prev()->tailroom();
return coalesceWithHeadroomTailroom(newHeadroom, newTailroom);
}
/**
* This is similar to the coalesce() method, except this allows to set a
* headroom and tailroom after coalescing.
*
* Returns ByteRange that points to the data IOBuf stores.
*/
ByteRange coalesceWithHeadroomTailroom(
std::size_t newHeadroom,
std::size_t newTailroom) {
if (isChained()) { if (isChained()) {
coalesceSlow(); coalesceAndReallocate(
newHeadroom, computeChainDataLength(), this, newTailroom);
} }
return ByteRange(data_, length_); return ByteRange(data_, length_);
} }
...@@ -1169,12 +1184,28 @@ class IOBuf { ...@@ -1169,12 +1184,28 @@ class IOBuf {
*/ */
std::unique_ptr<IOBuf> cloneCoalesced() const; std::unique_ptr<IOBuf> cloneCoalesced() const;
/**
* This is similar to the cloneCoalesced() method, except this allows to set a
* headroom and tailroom for the new IOBuf.
*/
std::unique_ptr<IOBuf> cloneCoalescedWithHeadroomTailroom(
std::size_t newHeadroom,
std::size_t newTailroom) const;
/** /**
* Similar to cloneCoalesced(). But returns IOBuf by value rather than * Similar to cloneCoalesced(). But returns IOBuf by value rather than
* heap-allocating it. * heap-allocating it.
*/ */
IOBuf cloneCoalescedAsValue() const; IOBuf cloneCoalescedAsValue() const;
/**
* This is similar to the cloneCoalescedAsValue() method, except this allows
* to set a headroom and tailroom for the new IOBuf.
*/
IOBuf cloneCoalescedAsValueWithHeadroomTailroom(
std::size_t newHeadroom,
std::size_t newTailroom) const;
/** /**
* Similar to Clone(). But use other as the head node. Other nodes in the * Similar to Clone(). But use other as the head node. Other nodes in the
* chain (if any) will be allocted on heap. * chain (if any) will be allocted on heap.
......
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