-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-120754: Refactor I/O modules to stash whole stat result rather than individual members #123412
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
508aa9d
9d849ce
bfcfcf2
3122665
8f5cfe4
d18a82d
c55d10e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,8 +74,7 @@ typedef struct { | |
| signed int seekable : 2; /* -1 means unknown */ | ||
| unsigned int closefd : 1; | ||
| char finalizing; | ||
| unsigned int blksize; | ||
| Py_off_t estimated_size; | ||
| struct _Py_stat_struct *stat_atopen; | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| PyObject *weakreflist; | ||
| PyObject *dict; | ||
| } fileio; | ||
|
|
@@ -199,8 +198,7 @@ fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
| self->writable = 0; | ||
| self->appending = 0; | ||
| self->seekable = -1; | ||
| self->blksize = 0; | ||
| self->estimated_size = -1; | ||
| self->stat_atopen = NULL; | ||
| self->closefd = 1; | ||
| self->weakreflist = NULL; | ||
| } | ||
|
|
@@ -256,7 +254,6 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, | |
| #elif !defined(MS_WINDOWS) | ||
| int *atomic_flag_works = NULL; | ||
| #endif | ||
| struct _Py_stat_struct fdfstat; | ||
| int fstat_result; | ||
| int async_err = 0; | ||
|
|
||
|
|
@@ -454,9 +451,13 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, | |
| #endif | ||
| } | ||
|
|
||
| self->blksize = DEFAULT_BUFFER_SIZE; | ||
| self->stat_atopen = PyMem_New(struct _Py_stat_struct, 1); | ||
| if (self->stat_atopen == NULL) { | ||
| PyErr_Format(PyExc_MemoryError, "Unable to allocate space for stat result"); | ||
cmaloney marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| goto error; | ||
| } | ||
| Py_BEGIN_ALLOW_THREADS | ||
| fstat_result = _Py_fstat_noraise(self->fd, &fdfstat); | ||
| fstat_result = _Py_fstat_noraise(self->fd, self->stat_atopen); | ||
| Py_END_ALLOW_THREADS | ||
| if (fstat_result < 0) { | ||
| /* Tolerate fstat() errors other than EBADF. See Issue #25717, where | ||
|
|
@@ -471,25 +472,21 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, | |
| #endif | ||
| goto error; | ||
| } | ||
|
|
||
| PyMem_Free(self->stat_atopen); | ||
| self->stat_atopen = NULL; | ||
| } | ||
| else { | ||
| #if defined(S_ISDIR) && defined(EISDIR) | ||
| /* On Unix, open will succeed for directories. | ||
| In Python, there should be no file objects referring to | ||
| directories, so we need a check. */ | ||
| if (S_ISDIR(fdfstat.st_mode)) { | ||
| if (S_ISDIR(self->stat_atopen->st_mode)) { | ||
| errno = EISDIR; | ||
| PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj); | ||
| goto error; | ||
| } | ||
| #endif /* defined(S_ISDIR) */ | ||
| #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE | ||
| if (fdfstat.st_blksize > 1) | ||
| self->blksize = fdfstat.st_blksize; | ||
| #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */ | ||
| if (fdfstat.st_size < PY_SSIZE_T_MAX) { | ||
| self->estimated_size = (Py_off_t)fdfstat.st_size; | ||
| } | ||
| } | ||
|
|
||
| #if defined(MS_WINDOWS) || defined(__CYGWIN__) | ||
|
|
@@ -521,6 +518,10 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, | |
| internal_close(self); | ||
| _PyErr_ChainExceptions1(exc); | ||
| } | ||
| if (self->stat_atopen != NULL) { | ||
| PyMem_Free(self->stat_atopen); | ||
| self->stat_atopen = NULL; | ||
| } | ||
|
|
||
| done: | ||
| #ifdef MS_WINDOWS | ||
|
|
@@ -553,6 +554,10 @@ fileio_dealloc(fileio *self) | |
| if (_PyIOBase_finalize((PyObject *) self) < 0) | ||
| return; | ||
| _PyObject_GC_UNTRACK(self); | ||
| if (self->stat_atopen != NULL) { | ||
| PyMem_Free(self->stat_atopen); | ||
| self->stat_atopen = NULL; | ||
| } | ||
| if (self->weakreflist != NULL) | ||
| PyObject_ClearWeakRefs((PyObject *) self); | ||
| (void)fileio_clear(self); | ||
|
|
@@ -725,7 +730,12 @@ _io_FileIO_readall_impl(fileio *self) | |
| return err_closed(); | ||
| } | ||
|
|
||
| end = self->estimated_size; | ||
| if (self->stat_atopen != NULL && self->stat_atopen->st_size < _PY_READ_MAX) { | ||
| end = (Py_off_t)self->stat_atopen->st_size; | ||
| } | ||
| else { | ||
| end = -1; | ||
| } | ||
| if (end <= 0) { | ||
| /* Use a default size and resize as needed. */ | ||
| bufsize = SMALLCHUNK; | ||
|
|
@@ -1094,11 +1104,13 @@ _io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj) | |
| return NULL; | ||
| } | ||
|
|
||
| /* Sometimes a large file is truncated. While estimated_size is used as a | ||
| estimate, that it is much larger than the actual size can result in a | ||
| significant over allocation and sometimes a MemoryError / running out of | ||
| memory. */ | ||
| self->estimated_size = pos; | ||
| /* Since the file was truncated, its size at open is no longer accurate | ||
| as an estimate. Clear out the stat result, and rely on dynamic resize | ||
vstinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| code if a readall is requested. */ | ||
| if (self->stat_atopen != NULL) { | ||
| PyMem_Free(self->stat_atopen); | ||
| self->stat_atopen = NULL; | ||
| } | ||
|
|
||
| return posobj; | ||
| } | ||
|
|
@@ -1229,16 +1241,27 @@ get_mode(fileio *self, void *closure) | |
| return PyUnicode_FromString(mode_string(self)); | ||
| } | ||
|
|
||
| static PyObject * | ||
| get_blksize(fileio *self, void *closure) | ||
| { | ||
| #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE | ||
| if (self->stat_atopen != NULL && self->stat_atopen->st_blksize > 1) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do wonder how realistic the st_blksize values, when available, are for performance purposes, I guess we'll find out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR should not change the buffer size, does it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #117151 (comment) investigated Have with the refactors + optimizations been watching for new issues. Are finding some as people test |
||
| return PyLong_FromLong(self->stat_atopen->st_blksize); | ||
| } | ||
| #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */ | ||
| return PyLong_FromLong(DEFAULT_BUFFER_SIZE); | ||
| } | ||
|
|
||
| static PyGetSetDef fileio_getsetlist[] = { | ||
| {"closed", (getter)get_closed, NULL, "True if the file is closed"}, | ||
| {"closefd", (getter)get_closefd, NULL, | ||
| "True if the file descriptor will be closed by close()."}, | ||
| {"mode", (getter)get_mode, NULL, "String giving the file mode"}, | ||
| {"_blksize", (getter)get_blksize, NULL, "Stat st_blksize if available"}, | ||
| {NULL}, | ||
| }; | ||
|
|
||
| static PyMemberDef fileio_members[] = { | ||
| {"_blksize", Py_T_UINT, offsetof(fileio, blksize), 0}, | ||
| {"_finalizing", Py_T_BOOL, offsetof(fileio, finalizing), 0}, | ||
| {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(fileio, weakreflist), Py_READONLY}, | ||
| {"__dictoffset__", Py_T_PYSSIZET, offsetof(fileio, dict), Py_READONLY}, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.