https://github.com/python/cpython/commit/8fa124868519eeda0a6dfe9191ceabd708d84fa7
commit: 8fa124868519eeda0a6dfe9191ceabd708d84fa7
branch: main
author: morotti <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-04-23T18:51:20+03:00
summary:

gh-117151: optimize BufferedWriter(), do not buffer writes that are the buffer 
size (GH-118037)

BufferedWriter() was buffering calls that are the exact same size as the 
buffer. it's a very common case to read/write in blocks of the exact buffer 
size.

it's pointless to copy a full buffer, it's costing extra memory copy and the 
full buffer will have to be written in the next call anyway.

Co-authored-by: rmorotti <[email protected]>

files:
M Modules/_io/bufferedio.c

diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 4133d3438253dd..aa52711941d374 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -2092,7 +2092,7 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer 
*buffer)
         self->raw_pos = 0;
     }
     avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, 
Py_ssize_t);
-    if (buffer->len <= avail) {
+    if (buffer->len <= avail && buffer->len < self->buffer_size) {
         memcpy(self->buffer + self->pos, buffer->buf, buffer->len);
         if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) {
             self->write_pos = self->pos;
@@ -2161,7 +2161,7 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer 
*buffer)
     /* Then write buf itself. At this point the buffer has been emptied. */
     remaining = buffer->len;
     written = 0;
-    while (remaining > self->buffer_size) {
+    while (remaining >= self->buffer_size) {
         Py_ssize_t n = _bufferedwriter_raw_write(
             self, (char *) buffer->buf + written, buffer->len - written);
         if (n == -1) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to