Skip to content

Commit

Permalink
pythongh-117151: optimize BufferedWriter(), do not buffer writes that…
Browse files Browse the repository at this point in the history
… are the buffer size (pythonGH-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]>
  • Loading branch information
morotti and rmmancom authored Apr 23, 2024
1 parent 23950be commit 8fa1248
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8fa1248

Please sign in to comment.