mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
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 <romain.morotti@man.com>
This commit is contained in:
parent
23950beff8
commit
8fa1248685
1 changed files with 2 additions and 2 deletions
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue