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

GH-120754: Disable buffering in Path.read_bytes#122111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
GH-120754: Disable buffering in Path.read_bytes
`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```
  • Loading branch information
@cmaloney
cmaloney committedJul 22, 2024
commitdac5dd9f15f960f808a9bd547fa4271db6478b5e
2 changes: 1 addition & 1 deletionLib/pathlib/_abc.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -576,7 +576,7 @@ def read_bytes(self):
"""
Open the file in bytes mode, read it, and close the file.
"""
with self.open(mode='rb') as f:
with self.open(mode='rb', buffering=0) as f:
return f.read()

def read_text(self, encoding=None, errors=None, newline=None):
Expand Down
2 changes: 1 addition & 1 deletionLib/test/test_pathlib/test_pathlib_abc.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1465,7 +1465,7 @@ def stat(self, *, follow_symlinks=True):

def open(self, mode='r', buffering=-1, encoding=None,
errors=None, newline=None):
if buffering != -1:
if buffering != -1 and not (buffering == 0 and 'b' in mode):
raise NotImplementedError
path_obj = self.resolve()
path = str(path_obj)
Expand Down

[8]ページ先頭

©2009-2026 Movatter.jp