From 8dd8df7c82baf5d43b8a757cac819b3bada0b9b8 Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Sat, 22 Feb 2025 00:20:00 -0800 Subject: [PATCH] Improve iobuf -> numpy conversion latency Summary: roticv provided this change to bypass coalescing iobuf with `b"".join(iobuf)` and providing the iobuf itself as the buffer to numpy array Reviewed By: roticv Differential Revision: D69883091 fbshipit-source-id: 64f6268237f2e8de2b221ba3e1d0bf068c8e5f28 --- folly/python/iobuf.pxd | 1 + folly/python/iobuf.pyi | 1 + folly/python/iobuf.pyx | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/folly/python/iobuf.pxd b/folly/python/iobuf.pxd index f1ebbf177e6..34ab5455dc9 100644 --- a/folly/python/iobuf.pxd +++ b/folly/python/iobuf.pxd @@ -32,6 +32,7 @@ cdef extern from "folly/io/IOBuf.h" namespace "folly": size_t countChainElements() uint64_t computeChainDataLength() unique_ptr[cIOBuf] clone() + unique_ptr[cIOBuf] cloneCoalesced() cIOBuf* prev() cIOBuf* next() void insertAfterThisOne(unique_ptr[cIOBuf]&& ciobuf) diff --git a/folly/python/iobuf.pyi b/folly/python/iobuf.pyi index 0893c1cdf31..df2d4ae716c 100644 --- a/folly/python/iobuf.pyi +++ b/folly/python/iobuf.pyi @@ -18,6 +18,7 @@ class IOBuf(Hashable): def __init__(self, buffer: Union[IOBuf, bytes, bytearray, memoryview]) -> None: ... def writable(self) -> bool: ... def clone(self) -> IOBuf: ... + def cloneCoalesced(self) -> IOBuf: ... @property def next(self) -> Optional[IOBuf]: ... @property diff --git a/folly/python/iobuf.pyx b/folly/python/iobuf.pyx index 046f30d56b0..044921b94fa 100644 --- a/folly/python/iobuf.pyx +++ b/folly/python/iobuf.pyx @@ -103,6 +103,10 @@ cdef class IOBuf: """ Clone the iobuf chain """ return from_unique_ptr(self._this.clone()) + def cloneCoalesced(self): + """ Clone the iobuf chain """ + return from_unique_ptr(self._this.cloneCoalesced()) + @property def next(self): _next = self._this.next()