This is an automated email from the ASF dual-hosted git repository.

nicholasjiang pushed a commit to branch branch-0.6
in repository https://gitbox.apache.org/repos/asf/celeborn.git


The following commit(s) were added to refs/heads/branch-0.6 by this push:
     new 38eccb9fc [CELEBORN-2170] Refactor ByteBuffer's readToReadOnlyuffer 
interface
38eccb9fc is described below

commit 38eccb9fc3bc1ff9b5b681f41e48a161b17e92c5
Author: HolyLow <[email protected]>
AuthorDate: Mon Oct 13 19:28:47 2025 +0800

    [CELEBORN-2170] Refactor ByteBuffer's readToReadOnlyuffer interface
    
    ### What changes were proposed in this pull request?
    This PR refactors ByteBuffer's readToReadOnlyBuffer interface to make it 
more clear and robust. UTs are updated to test this interface.
    
    ### Why are the changes needed?
    To make ByteBuffer's readToReadOnlyBuffer interface more clear and robust.
    
    ### Does this PR introduce _any_ user-facing change?
    No.
    
    ### How was this patch tested?
    Compilation and UTs.
    
    Closes #3500 from 
HolyLow/issue/celeborn-2170-refactor-bytebuffer-readToReadOnly.
    
    Authored-by: HolyLow <[email protected]>
    Signed-off-by: SteNicholas <[email protected]>
    (cherry picked from commit 7c4f7b1665c125ab3b45fedf3f2d8546b2f64fae)
    Signed-off-by: SteNicholas <[email protected]>
---
 cpp/celeborn/memory/ByteBuffer.cpp           | 20 +++-----------------
 cpp/celeborn/memory/tests/ByteBufferTest.cpp | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/cpp/celeborn/memory/ByteBuffer.cpp 
b/cpp/celeborn/memory/ByteBuffer.cpp
index e35f662f8..4b415aed9 100644
--- a/cpp/celeborn/memory/ByteBuffer.cpp
+++ b/cpp/celeborn/memory/ByteBuffer.cpp
@@ -77,23 +77,9 @@ std::unique_ptr<folly::IOBuf> ByteBuffer::trimBuffer(
 
 std::unique_ptr<ReadOnlyByteBuffer> ReadOnlyByteBuffer::readToReadOnlyBuffer(
     const size_t len) const {
-  std::unique_ptr<folly::IOBuf> leftData = folly::IOBuf::create(0);
-  auto cnt = 0;
-  while (cnt < len) {
-    if (this->remainingSize() == 0) {
-      break;
-    }
-    std::unique_ptr<folly::IOBuf> newBlock =
-        std::move(this->cursor_->currentBuffer()->cloneOne());
-    newBlock->pop();
-    newBlock->trimStart(this->cursor_->getPositionInCurrentBuffer());
-    if (newBlock->length() > len - cnt) {
-      newBlock->trimEnd(newBlock->length() - (len - cnt));
-    }
-    this->cursor_->skip(newBlock->length());
-    cnt += newBlock->length();
-    leftData->appendToChain(std::move(newBlock));
-  }
+  std::unique_ptr<folly::IOBuf> leftData = nullptr;
+  this->cursor_->clone(leftData, len);
+
   return createReadOnly(std::move(leftData), isBigEndian_);
 }
 } // namespace memory
diff --git a/cpp/celeborn/memory/tests/ByteBufferTest.cpp 
b/cpp/celeborn/memory/tests/ByteBufferTest.cpp
index f6c4c6711..b0f6f6fef 100644
--- a/cpp/celeborn/memory/tests/ByteBufferTest.cpp
+++ b/cpp/celeborn/memory/tests/ByteBufferTest.cpp
@@ -219,7 +219,14 @@ TEST(ByteBufferTest, continuousBufferRead) {
   auto ioBuf = folly::IOBuf::wrapBuffer(data.get(), size);
 
   auto readBuffer = ByteBuffer::createReadOnly(std::move(ioBuf));
+  auto readBuffer2 = readBuffer->clone();
+
   testReadData(readBuffer.get(), size);
+
+  // Test readToReadOnlyBuffer.
+  auto toReadBuffer = readBuffer2->readToReadOnlyBuffer(size);
+  EXPECT_EQ(readBuffer2->remainingSize(), 0);
+  testReadData(toReadBuffer.get(), size);
 }
 
 TEST(ByteBufferTest, segmentedBufferRead) {
@@ -248,7 +255,14 @@ TEST(ByteBufferTest, segmentedBufferRead) {
   }
 
   auto readBuffer = ByteBuffer::createReadOnly(std::move(ioBuf));
+  auto readBuffer2 = readBuffer->clone();
+
   testReadData(readBuffer.get(), size);
+
+  // Test readToReadOnlyBuffer.
+  auto toReadBuffer = readBuffer2->readToReadOnlyBuffer(size);
+  EXPECT_EQ(readBuffer2->remainingSize(), 0);
+  testReadData(toReadBuffer.get(), size);
 }
 
 TEST(ByteBufferTest, writeBufferAndRead) {

Reply via email to