GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-120754: Reduce system calls in full-file readall case by cmaloney · Pull Request #120755 · python/cpython Conversation
|
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Could you add some tests? And share benchmark results compared against the main branch? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Is there a standard way to add tests for "this set of system calls is made" or "this many system calls is made"? I tried hunting through the existing tests but couldn't find anything like that or a good way to do that for underlying C code. Would definitely be nice to have a test around re: Benchmarking, I did some with a test program and included details in the initial commit: 78c4de0, wall clock on my dev machine changes were generally in the noise. Happy to work on running a more general suite. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I simply meant to test that the code still works correctly with the changes you made. Set up git worktree, build the main branch and |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
For testing, the existing test_fileio checks basic behavior of |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I ran pyperformance benchmark and didn't get any big swings / just noise. Writing a little pyperf benchmark around "read whole file: import pyperf from pathlib import Path def read_file(path_obj): path_obj.read_text() 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")) cmaloney/readall_faster main for my particular Mac |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks, this is excellent. cpython/Lib/test/test_subprocess.py Line 3437 in 0152dc4
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@hauntsaninja planning to try and make a separate PR for that (list item per what I think will be separate commits)
Then use that infrastructure here (So this PR will get a merge commit + new commit which updates the test for the less system calls). I don't think that needs a separate GH Issue to track, if it does let me know. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is there a standard way to add tests for "this set of system calls is made" or "this many system calls is made
The tests for IO are spread around multiple files but I think test_fileio is the best one for that. If you want to emulate the number of calls being made, you could try to align the Python implementation with the C implementation (which is usually what we try to achieve). Note that the python implementation calls read/lseek/fstat directly for FileIO, so you may also try to mock them as well. For the C implementation, yes, the strace alternative is probably the best, but I think it's a nice idea to see whether you could also improve the Python implementation itself.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
re: _pyio I'll look at how far its behavior is currently from _io when I add the system call test. I would like not to pull getting them to match into the scope of work for this PR. Longer term I would really like to make |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks good to me!
It might make sense to just use DEFAULT_BUFFER_SIZE for your threshold, especially so if #118144 is merged
I agree that pyio and a strace test can be done in another PR
I requested review from Serhiy in case he has time to take a look, if not I'll merge soon
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…e-120754.uF29sj.rst
|
Re: DEFAULT_BUFFER_SIZE, I actually experimented with "just allocate and try and try to read DEFAULT_BUFFER_SIZE always", and found that for both small and large files it was slower. Not entirely sure what the slowdown was, but led me to the "cache the size" approach which is uniformly faster. Definitely an interesting constant to raise, and I think fairly important on the write side. Would be curious to see numbers for read. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Updated with changes to make _pyio.FileIO system calls match, tested locally with added strace syscall test #121143 (diff on top of that PR to get to passing with these changes 0606677) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
size_t is too small (and read would cap it anyways) to read the whole file
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
A minor nit regarding the comments: I'm going to align them to the existing style used in this file; hope you don't mind :)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| unsigned int closefd : 1; | ||
| char finalizing; | ||
| unsigned int blksize; | ||
| Py_off_t size_estimated; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to use the same name in the C and Python implementation, I suggest to rename this member to: estimated_size.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| bufsize = _PY_READ_MAX; | ||
| } | ||
| else { | ||
| bufsize = Py_SAFE_DOWNCAST(end, Py_off_t, size_t) + 1; |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I don't think that this cast is safe, Py_off_t can be bigger than size_t. You should do something like:
bufsize = (size_t)Py_MIN(end, SIZE_MAX); bufsize++;
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I ran into issues in test_largefile on Windows x86 which caused me to add this. Py_off_t is long long on that while size_t is int
cpython/Modules/_io/_iomodule.h
Lines 95 to 106 in 4f1e1df
| #ifdef MS_WINDOWS | |
| /* Windows uses long long for offsets */ | |
| typedef long long Py_off_t; | |
| # define PyLong_AsOff_t PyLong_AsLongLong | |
| # define PyLong_FromOff_t PyLong_FromLongLong | |
| # define PY_OFF_T_MAX LLONG_MAX | |
| # define PY_OFF_T_MIN LLONG_MIN | |
| # define PY_OFF_T_COMPAT long long /* type compatible with off_t */ | |
| # define PY_PRIdOFF "lld" /* format to use for that type */ | |
| #else |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oop, misread this. The if end >= _PY_READ_MAX just before should catch this. (_PY_READ_MAX <= SIZE_MAX).
https://github.com/python/cpython/blob/main/Include/internal/pycore_fileutils.h#L65-L76
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, in fact the maximum is PY_SSIZE_T_MAX:
bufsize = (size_t)Py_MIN(end, PY_SSIZE_T_MAX); if (bufsize < PY_SSIZE_T_MAX) { bufsize++; }
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In this case, replace bufsize = Py_SAFE_DOWNCAST(end, Py_off_t, size_t) + 1; with just bufsize = (size_t)end + 1;. I just dislike Py_SAFE_DOWNCAST() macro, it's not safe, the name is misleading.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| if self._estimated_size <= 0: | ||
| bufsize = DEFAULT_BUFFER_SIZE | ||
| else: | ||
| bufsize = self._estimated_size + 1 |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of the "+1"? It may overallocate 1 byte which is inefficient.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The read loop currently needs to do a os.read() / _py_Read which is a single byte which returns 0 size to find the end of the file and exit the loop. The very beginning of that loop does a check for "if buffer is full, grow buffer" so not over-allocating by one byte results in a much bigger allocation by that. In the _io case it then shrinks it back down at the end, whereas in the _pyio case the EOF read is never appended.
Could avoid the extra byte by writing a specialized "read known size" (w/ fallback to "read until EOF"), but was trying to avoid making more variants of the read loop and limit risk a bit.
As an aside: the _pyio implementation seems to have a lot of extra memory allocation and copy in the default case because os.read() internally allocates a buffer which it then copies into its bytearray...
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'll work on renaming the members to be consistent tomorrow
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| bufsize = _PY_READ_MAX; | ||
| } | ||
| else { | ||
| bufsize = Py_SAFE_DOWNCAST(end, Py_off_t, size_t) + 1; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Oop, misread this. The if end >= _PY_READ_MAX just before should catch this. (_PY_READ_MAX <= SIZE_MAX).
https://github.com/python/cpython/blob/main/Include/internal/pycore_fileutils.h#L65-L76
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| if self._estimated_size <= 0: | ||
| bufsize = DEFAULT_BUFFER_SIZE | ||
| else: | ||
| bufsize = self._estimated_size + 1 |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The read loop currently needs to do a os.read() / _py_Read which is a single byte which returns 0 size to find the end of the file and exit the loop. The very beginning of that loop does a check for "if buffer is full, grow buffer" so not over-allocating by one byte results in a much bigger allocation by that. In the _io case it then shrinks it back down at the end, whereas in the _pyio case the EOF read is never appended.
Could avoid the extra byte by writing a specialized "read known size" (w/ fallback to "read until EOF"), but was trying to avoid making more variants of the read loop and limit risk a bit.
As an aside: the _pyio implementation seems to have a lot of extra memory allocation and copy in the default case because os.read() internally allocates a buffer which it then copies into its bytearray...
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Per review, update range checks to be more clear and accurate
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Merged, thank you. It's a nice optimization. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@cmaloney: Oh, test_largefile failed. Can you investigate? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Looks like just the C implementation ( |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@vstinner I think #121357 will fix the failure, although I'm unable to reproduce locally so far. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Speed up open().read() pattern by reducing the number of system calls
There was an error while loading. Please reload this page.