You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by xu...@apache.org on 2023/11/19 09:16:36 UTC

(incubator-opendal) branch main updated: refactor(oio): Polish IncomingAsyncBody::bytes (#3621)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 84dfbfb5f refactor(oio): Polish IncomingAsyncBody::bytes (#3621)
84dfbfb5f is described below

commit 84dfbfb5f4595dbdebdacc5b43305e6c5a697c2f
Author: Xuanwo <gi...@xuanwo.io>
AuthorDate: Sun Nov 19 17:16:31 2023 +0800

    refactor(oio): Polish IncomingAsyncBody::bytes (#3621)
---
 core/src/raw/http_util/body.rs | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/core/src/raw/http_util/body.rs b/core/src/raw/http_util/body.rs
index fec20f9ee..faaa3b2c4 100644
--- a/core/src/raw/http_util/body.rs
+++ b/core/src/raw/http_util/body.rs
@@ -105,12 +105,12 @@ impl IncomingAsyncBody {
 
     /// Consume the response to bytes.
     ///
-    /// This code is Inspired from hyper's [`to_bytes`](https://docs.rs/hyper/latest/hyper/body/fn.to_bytes.html).
+    /// This code is inspired from hyper's [`to_bytes`](https://docs.rs/hyper/0.14.23/hyper/body/fn.to_bytes.html).
     pub async fn bytes(mut self) -> Result<Bytes> {
         use oio::ReadExt;
 
         // If there's only 1 chunk, we can just return Buf::to_bytes()
-        let mut first = if let Some(buf) = self.next().await {
+        let first = if let Some(buf) = self.next().await {
             buf?
         } else {
             return Ok(Bytes::new());
@@ -119,11 +119,19 @@ impl IncomingAsyncBody {
         let second = if let Some(buf) = self.next().await {
             buf?
         } else {
-            return Ok(first.copy_to_bytes(first.remaining()));
+            return Ok(first);
         };
 
         // With more than 1 buf, we gotta flatten into a Vec first.
-        let cap = first.remaining() + second.remaining() + self.size.unwrap_or_default() as usize;
+        let cap = if let Some(size) = self.size {
+            // The convert from u64 to usize could fail, but it's unlikely.
+            // Let's just make it overflow.
+            size as usize
+        } else {
+            // It's highly possible that we have more data to read.
+            // Add extra 16K buffer to avoid another allocation.
+            first.remaining() + second.remaining() + 16 * 1024
+        };
         let mut vec = Vec::with_capacity(cap);
         vec.put(first);
         vec.put(second);