Skip to content

gh-135474: Don't run the GC in the middle of longobject operations #135475

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Include/internal/pycore_pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ extern int _PyUnicodeError_GetParams(
Py_ssize_t *slen,
int as_bytes);


int _PyErr_CheckSignalsWithoutGC(void);

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 3 additions & 3 deletions Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The garbage collector no longer runs in the middle of a long-running int addition/subtraction/multiplication. This should have little real effect on real-world code, while simplifying CPython's internal invariants. This may also speed up long-running int operations.
20 changes: 20 additions & 0 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,26 @@ PyErr_CheckSignals(void)
return _PyErr_CheckSignalsTstate(tstate);
}

/* Same as PyErr_CheckSignals but does not run the GC.
This can be safely used if you are certain that no allocation
is done inside the long-running C code.
*/
int
_PyErr_CheckSignalsWithoutGC(void)
{
PyThreadState *tstate = _PyThreadState_GET();

#if defined(Py_REMOTE_DEBUG) && defined(Py_SUPPORTS_REMOTE_DEBUG)
_PyRunRemoteDebugger(tstate);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can do anything, so could easily cause the GC to run.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah drats. You're right. I will fix this by fixing the specialization then.

#endif

if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
return 0;
}

return _PyErr_CheckSignalsTstate(tstate);
}


/* Declared in cpython/pyerrors.h */
int
Expand Down
2 changes: 1 addition & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ maybe_small_long(PyLongObject *v)

#define SIGCHECK(PyTryBlock) \
do { \
if (PyErr_CheckSignals()) PyTryBlock \
if (_PyErr_CheckSignalsWithoutGC()) PyTryBlock \
} while(0)

/* Normalize (remove leading zeros from) an int object.
Expand Down
6 changes: 0 additions & 6 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
"_PyLong_IsNegative",
"_PyLong_IsNonNegativeCompact",
"_PyLong_IsZero",
"_PyLong_Add",
"_PyLong_Multiply",
"_PyLong_Subtract",
"_PyManagedDictPointer_IsValues",
"_PyObject_GC_IS_SHARED",
"_PyObject_GC_IS_TRACKED",
Expand Down
Loading