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

xuanwo pushed a commit to branch polish-buffer
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git

commit 37d4b2afc5cc3f5db4f568e8fb73e213d83b6f96
Author: Xuanwo <[email protected]>
AuthorDate: Tue Sep 12 17:03:27 2023 +0800

    Polish write
    
    Signed-off-by: Xuanwo <[email protected]>
---
 core/benches/oio/utils.rs             |  2 --
 core/src/raw/oio/buf/chunked_bytes.rs | 17 +++++++++++++++--
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/core/benches/oio/utils.rs b/core/benches/oio/utils.rs
index af8c9a205..338587062 100644
--- a/core/benches/oio/utils.rs
+++ b/core/benches/oio/utils.rs
@@ -34,8 +34,6 @@ impl oio::Write for BlackHoleWriter {
         _: &mut Context<'_>,
         bs: &dyn oio::WriteBuf,
     ) -> Poll<opendal::Result<usize>> {
-        // Simulate the write operation.
-        let _ = bs.bytes(bs.remaining());
         Poll::Ready(Ok(bs.remaining()))
     }
 
diff --git a/core/src/raw/oio/buf/chunked_bytes.rs 
b/core/src/raw/oio/buf/chunked_bytes.rs
index ae8bcb6d9..82447dc62 100644
--- a/core/src/raw/oio/buf/chunked_bytes.rs
+++ b/core/src/raw/oio/buf/chunked_bytes.rs
@@ -103,6 +103,18 @@ impl ChunkedBytes {
     pub fn push(&mut self, mut bs: Bytes) {
         self.size += bs.len();
 
+        // Optimization: if active is empty, we can push to frozen directly if 
possible.
+        if self.active.is_empty() {
+            let aligned_size = bs.len() - bs.len() % self.chunk_size;
+            if aligned_size > 0 {
+                self.frozen.push_back(bs.split_to(aligned_size));
+            }
+            if !bs.is_empty() {
+                self.active.extend_from_slice(&bs);
+            }
+            return;
+        }
+
         // Try to fill bytes into active first.
         let remaining = self.chunk_size.saturating_sub(self.active.len());
         if remaining > 0 {
@@ -116,8 +128,9 @@ impl ChunkedBytes {
         }
 
         // Split remaining bytes into chunks.
-        while bs.len() >= self.chunk_size {
-            self.frozen.push_back(bs.split_to(self.chunk_size));
+        let aligned_size = bs.len() - bs.len() % self.chunk_size;
+        if aligned_size > 0 {
+            self.frozen.push_back(bs.split_to(aligned_size));
         }
 
         // Append to active if there are remaining bytes.

Reply via email to