From 3668d1ab0e2567e0df1b1d26f226a901c57f3707 Mon Sep 17 00:00:00 2001 From: rmorotti Date: Thu, 21 Mar 2024 15:07:56 +0000 Subject: [PATCH] gh-117151: IO performance improvement, increase io.DEFAULT_BUFFER_SIZE to 128k, fix open() to use max(st_blksize, io.DEFAULT_BUFFER_SIZE) --- Lib/_pyio.py | 6 +++--- .../2024-04-30-14-03-09.gh-issue-117151.yt2H8c.rst | 3 +++ Modules/_io/_iomodule.h | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-04-30-14-03-09.gh-issue-117151.yt2H8c.rst diff --git a/Lib/_pyio.py b/Lib/_pyio.py index a3fede699218a15..7979d5766668917 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -23,8 +23,8 @@ valid_seek_flags.add(os.SEEK_HOLE) valid_seek_flags.add(os.SEEK_DATA) -# open() uses st_blksize whenever we can -DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes +# open() uses max(st_blksize, io.DEFAULT_BUFFER_SIZE) when st_blksize is available +DEFAULT_BUFFER_SIZE = 128 * 1024 # bytes # NOTE: Base classes defined here are registered with the "official" ABCs # defined in io.py. We don't use real inheritance though, because we don't want @@ -249,7 +249,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None, pass else: if bs > 1: - buffering = bs + buffering = max(bs, DEFAULT_BUFFER_SIZE) if buffering < 0: raise ValueError("invalid buffering size") if buffering == 0: diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-04-30-14-03-09.gh-issue-117151.yt2H8c.rst b/Misc/NEWS.d/next/Core and Builtins/2024-04-30-14-03-09.gh-issue-117151.yt2H8c.rst new file mode 100644 index 000000000000000..f0574bbc16c37d0 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-04-30-14-03-09.gh-issue-117151.yt2H8c.rst @@ -0,0 +1,3 @@ +increase io.DEFAULT_BUFFER_SIZE from 4k/8k to 128k. fix open on Linux to use +max(io.DEFAULT_BUFFER_SIZE, device block size) rather than the device block +size. improve I/O performance by 3 to 5 times. patch by Romain Morotti. diff --git a/Modules/_io/_iomodule.h b/Modules/_io/_iomodule.h index afd638a120ba085..18cf20edf26f7d9 100644 --- a/Modules/_io/_iomodule.h +++ b/Modules/_io/_iomodule.h @@ -78,7 +78,7 @@ extern Py_ssize_t _PyIO_find_line_ending( */ extern int _PyIO_trap_eintr(void); -#define DEFAULT_BUFFER_SIZE (8 * 1024) /* bytes */ +#define DEFAULT_BUFFER_SIZE (128 * 1024) /* bytes */ /* * Offset type for positioning.