gh-117151: optimize BufferedWriter(), do not buffer writes that are the buffer size by morotti · Pull Request #118037 · python/cpython (original) (raw)

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

Hello,

I am having a look at the buffering code as part of the linked discussion, #gh-117151
One thing that was puzzling me is finding the buffering code slightly slower than the non-buffering code, given the same conditions, so I started having a deeper look into it.

Could I get a second pair of eyes on this old piece of code?
BufferedWriter() is 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.
I think this might actually be the primary use case, reading in chunks from disk/network and writing to disk.

I think it shouldn't buffer in that case. It's costing an extra memory copy and the buffer will be full and have to be written in the next call anyway.

Thoughts?