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


##########
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;
     }

Review Comment:
   This check can be removed if you rework the logic below a bit:
   ```c++
       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;
       }
   ```



-- 
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