pitrou commented on code in PR #37460:
URL: https://github.com/apache/arrow/pull/37460#discussion_r1311608609


##########
cpp/src/arrow/io/buffered.cc:
##########
@@ -373,33 +378,42 @@ class BufferedInputStream::Impl : public BufferedBase {
       return Status::Invalid("Bytes to read must be positive. Received:", 
nbytes);
     }
 
-    if (nbytes < buffer_size_) {
-      // Pre-buffer for small reads
-      RETURN_NOT_OK(BufferIfNeeded());
+    // 1. First consume pre-buffered data.
+    int64_t pre_buffer_copy_bytes = std::min(nbytes, bytes_buffered_);
+    if (pre_buffer_copy_bytes > 0) {
+      memcpy(out, buffer_data_ + buffer_pos_, pre_buffer_copy_bytes);
+      ConsumeBuffer(pre_buffer_copy_bytes);
+    }
+    int64_t remaining_bytes = nbytes - pre_buffer_copy_bytes;
+    if (remaining_bytes == 0) {
+      return nbytes;
     }
 
-    if (nbytes > bytes_buffered_) {
-      // Copy buffered bytes into out, then read rest
-      memcpy(out, buffer_data_ + buffer_pos_, bytes_buffered_);
-
-      int64_t bytes_to_read = nbytes - bytes_buffered_;
-      if (raw_read_bound_ >= 0) {
-        bytes_to_read = std::min(bytes_to_read, raw_read_bound_ - 
raw_read_total_);
+    DCHECK_EQ(0, bytes_buffered_);
+    if (raw_read_bound_ >= 0) {
+      remaining_bytes = std::min(remaining_bytes, raw_read_bound_ - 
raw_read_total_);
+      if (remaining_bytes == 0) {
+        return pre_buffer_copy_bytes;
       }
-      ARROW_ASSIGN_OR_RAISE(
-          int64_t bytes_read,
-          raw_->Read(bytes_to_read, reinterpret_cast<uint8_t*>(out) + 
bytes_buffered_));
-      raw_read_total_ += bytes_read;
+    }
 
-      // Do not make assumptions about the raw stream position
-      raw_pos_ = -1;
-      bytes_read += bytes_buffered_;
+    // 2. Read from storage.
+    if (remaining_bytes > buffer_size_) {

Review Comment:
   Nit: no need to fill the buffer if we are going to exhaust it entirely
   ```suggestion
       if (remaining_bytes >= buffer_size_) {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to