Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit35d8ac7

Browse files
authored
GH-120754: Disable buffering in Path.read_bytes (#122111)
`Path.read_bytes()` is used to read a whole file. buffering /BufferedIO is focused around making small, possibly interleaved,read/write efficient which doesn't add value in this case.On my Mac, running the benchmark:```pythonimport pyperffrom pathlib import Pathdef read_all(all_paths): for p in all_paths: p.read_bytes()def read_file(path_obj): path_obj.read_bytes()all_rst = list(Path("Doc").glob("**/*.rst"))all_py = list(Path(".").glob("**/*.py"))assert all_rst, "Should have found rst files"assert all_py, "Should have found python source files"runner = pyperf.Runner()runner.bench_func("read_file_small", read_file, Path("Doc/howto/clinic.rst"))runner.bench_func("read_file_large", read_file, Path("Doc/c-api/typeobj.rst"))```before:```python.....................read_file_small: Mean +- std dev: 6.80 us +- 0.07 us.....................read_file_large: Mean +- std dev: 10.8 us +- 0.2 us````after:```python.....................read_file_small: Mean +- std dev: 5.67 us +- 0.05 us.....................read_file_large: Mean +- std dev: 9.77 us +- 0.52 us```
1 parent8ef358d commit35d8ac7

File tree

3 files changed

+3
-2
lines changed

3 files changed

+3
-2
lines changed

‎Lib/pathlib/_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def read_bytes(self):
585585
"""
586586
Open the file in bytes mode, read it, and close the file.
587587
"""
588-
withself.open(mode='rb')asf:
588+
withself.open(mode='rb',buffering=0)asf:
589589
returnf.read()
590590

591591
defread_text(self,encoding=None,errors=None,newline=None):

‎Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ def stat(self, *, follow_symlinks=True):
14641464

14651465
defopen(self,mode='r',buffering=-1,encoding=None,
14661466
errors=None,newline=None):
1467-
ifbuffering!=-1:
1467+
ifbuffering!=-1andnot (buffering==0and'b'inmode):
14681468
raiseNotImplementedError
14691469
path_obj=self.resolve()
14701470
path=str(path_obj)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``Pathlib.read_bytes`` no longer opens the file in Python's buffered I/O mode. This reduces overheads as the code reads a file in whole leading to a modest speedup.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp