|
23 | 23 | valid_seek_flags.add(os.SEEK_HOLE) |
24 | 24 | valid_seek_flags.add(os.SEEK_DATA) |
25 | 25 |
|
26 | | -# open() uses st_blksize whenever we can |
27 | | -DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes |
| 26 | +# open() uses max(min(blocksize, 8 MiB), DEFAULT_BUFFER_SIZE) |
| 27 | +# when the device block size is available. |
| 28 | +DEFAULT_BUFFER_SIZE = 128 * 1024 # bytes |
28 | 29 |
|
29 | 30 | # NOTE: Base classes defined here are registered with the "official" ABCs |
30 | 31 | # defined in io.py. We don't use real inheritance though, because we don't want |
@@ -123,10 +124,10 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None, |
123 | 124 | the size of a fixed-size chunk buffer. When no buffering argument is |
124 | 125 | given, the default buffering policy works as follows: |
125 | 126 |
|
126 | | - * Binary files are buffered in fixed-size chunks; the size of the buffer |
127 | | - is chosen using a heuristic trying to determine the underlying device's |
128 | | - "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`. |
129 | | - On many systems, the buffer will typically be 4096 or 8192 bytes long. |
| 127 | + * Binary files are buffered in fixed-size chunks; the size of the buffer |
| 128 | + is max(min(blocksize, 8 MiB), DEFAULT_BUFFER_SIZE) |
| 129 | + when the device block size is available. |
| 130 | + On most systems, the buffer will typically be 128 kilobytes long. |
130 | 131 |
|
131 | 132 | * "Interactive" text files (files for which isatty() returns True) |
132 | 133 | use line buffering. Other text files use the policy described above |
@@ -242,7 +243,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None, |
242 | 243 | buffering = -1 |
243 | 244 | line_buffering = True |
244 | 245 | if buffering < 0: |
245 | | - buffering = raw._blksize |
| 246 | + buffering = max(min(raw._blksize, 8192 * 1024), DEFAULT_BUFFER_SIZE) |
246 | 247 | if buffering < 0: |
247 | 248 | raise ValueError("invalid buffering size") |
248 | 249 | if buffering == 0: |
|
0 commit comments