summaryrefslogtreecommitdiffstats
path: root/Modules/gcmodule.c
Commit message (Collapse)AuthorAgeFilesLines
* gh-122943: Rework support of var-positional parameter in Argument Clinic ↵Serhiy Storchaka2024-11-071-29/+10
| | | | | | | | | | | | | | | | | | (GH-122945) Move creation of a tuple for var-positional parameter out of _PyArg_UnpackKeywordsWithVararg(). Merge _PyArg_UnpackKeywordsWithVararg() with _PyArg_UnpackKeywords(). Add a new parameter in _PyArg_UnpackKeywords(). The "parameters" and "converters" attributes of ParseArgsCodeGen no longer contain the var-positional parameter. It is now available as the "varpos" attribute. Optimize code generation for var-positional parameter and reuse the same generating code for functions with and without keyword parameters. Add special converters for var-positional parameter. "tuple" represents it as a Python tuple and "array" represents it as a continuous array of PyObject*. "object" is a temporary alias of "tuple".
* gh-90370: Argument Clinic: avoid temporary tuple creation for varargs (#126064)Sergey B Kirpichev2024-10-311-9/+30
| | | | | | | Avoid temporary tuple creation when all arguments either positional-only or vararg. Objects/setobject.c and Modules/gcmodule.c adapted. This fixes slight performance regression for set methods, introduced by gh-115112.
* gh-116322: Add Py_mod_gil module slot (#116882)Brett Simmers2024-05-031-0/+1
| | | | | | | | | | | | | | This PR adds the ability to enable the GIL if it was disabled at interpreter startup, and modifies the multi-phase module initialization path to enable the GIL when loading a module, unless that module's spec includes a slot indicating it can run safely without the GIL. PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148. A warning will be issued up to once per interpreter for the first GIL-using module that is loaded. If `-v` is given, a shorter message will be printed to stderr every time a GIL-using module is loaded (including the first one that issues a warning).
* GH-117108: Change the size of the GC increment to about 1% of the total heap ↵Mark Shannon2024-03-221-1/+1
| | | | size. (GH-117120)
* GH-108362: Incremental Cycle GC (GH-116206)Mark Shannon2024-03-201-15/+10
|
* gh-112529: Simplify PyObject_GC_IsTracked and PyObject_GC_IsFinalized (#114732)Sam Gross2024-02-281-19/+10
|
* gh-112529: Make the GC scheduling thread-safe (#114880)Sam Gross2024-02-161-0/+10
| | | | | | | | | | The GC keeps track of the number of allocations (less deallocations) since the last GC. This buffers the count in thread-local state and uses atomic operations to modify the per-interpreter count. The thread-local buffering avoids contention on shared state. A consequence is that the GC scheduling is not as precise, so "test_sneaky_frame_object" is skipped because it requires that the GC be run exactly after allocating a frame object.
* GH-108362: Revert "GH-108362: Incremental GC implementation (GH-108038)" ↵Mark Shannon2024-02-071-9/+14
| | | | | | | (#115132) Revert "GH-108362: Incremental GC implementation (GH-108038)" This reverts commit 36518e69d74607e5f094ce55286188e4545a947d.
* gh-112529: Stop the world around gc.get_referents (#114823)Sam Gross2024-02-061-13/+28
| | | | | | | We do not want to add locking in `tp_traverse` slot implementations. Instead, stop the world when calling `gc.get_referents`. Note that the the stop the world call is a no-op in the default build. Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
* GH-108362: Incremental GC implementation (GH-108038)Mark Shannon2024-02-051-14/+9
|
* gh-113755: Fully adapt gcmodule.c to Argument Clinic (#113756)Erlend E. Aasland2024-01-081-26/+51
| | | | | | | Adapt the following functions to Argument Clinic: - gc.set_threshold - gc.get_referrers - gc.get_referents
* gh-113688: Split up gcmodule.c (gh-113715)Sam Gross2024-01-051-1959/+32
| | | | | This splits part of Modules/gcmodule.c of into Python/gc.c, which now contains the core garbage collection implementation. The Python module remain in the Modules/gcmodule.c file.
* gh-112529: Use atomic operations for `gcstate->collecting` (#112533)Sam Gross2023-12-111-184/+168
| | | | | | | | | | | | | | | | * gh-112529: Use atomic operations for `gcstate->collecting` The `collecting` field in `GCState` is used to prevent overlapping garbage collections within the same interpreter. This is updated to use atomic operations in order to be thread-safe in `--disable-gil` builds. The GC code is refactored a bit to support this. More of the logic is pushed down to `gc_collect_main()` so that we can safely order the logic setting `collecting`, the selection of the generation, and the invocation of callbacks with respect to the atomic operations and the (future) stop-the-world pauses. The change uses atomic operations for both `--disable-gil` and the default build (with the GIL) to avoid extra `#ifdef` guards and ease the maintenance burden.
* gh-111178: Define `visitproc` callback functions properly and remove ↵Christopher Chavez2023-12-061-9/+13
| | | | unnecessary casts in gcmodule.c (#112687)
* gh-111777: Fix assertion errors on incorrectly still-tracked GC object ↵T. Wouters2023-11-121-1/+3
| | | | | | | | | | destruction (#111778) In PyObject_GC_Del, in Py_DEBUG mode, when warning about GC objects that were not properly untracked before starting destruction, take care to untrack the object _before_ warning, to avoid triggering a GC run and causing the problem the code tries to warn about. Also make sure to save and restore any pending exceptions, which the warning would otherwise clobber or trigger an assertion error on.
* gh-108082: Use PyErr_FormatUnraisable() (GH-111580)Serhiy Storchaka2023-11-021-5/+5
| | | | | | Replace most of calls of _PyErr_WriteUnraisableMsg() and some calls of PyErr_WriteUnraisable(NULL) with PyErr_FormatUnraisable(). Co-authored-by: Victor Stinner <vstinner@python.org>
* GH-109369: Merge all eval-breaker flags and monitoring version into one ↵Mark Shannon2023-10-041-5/+2
| | | | word. (GH-109846)
* gh-108753: Enhance pystats (#108754)Victor Stinner2023-09-061-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Statistics gathering is now off by default. Use the "-X pystats" command line option or set the new PYTHONSTATS environment variable to 1 to turn statistics gathering on at Python startup. Statistics are no longer dumped at exit if statistics gathering was off or statistics have been cleared. Changes: * Add PYTHONSTATS environment variable. * sys._stats_dump() now returns False if statistics are not dumped because they are all equal to zero. * Add PyConfig._pystats member. * Add tests on sys functions and on setting PyConfig._pystats to 1. * Add Include/cpython/pystats.h and Include/internal/pycore_pystats.h header files. * Rename '_py_stats' variable to '_Py_stats'. * Exclude Include/cpython/pystats.h from the Py_LIMITED_API. * Move pystats.h include from object.h to Python.h. * Add _Py_StatsOn() and _Py_StatsOff() functions. Remove '_py_stats_struct' variable from the API: make it static in specialize.c. * Document API in Include/pystats.h and Include/cpython/pystats.h. * Complete pystats documentation in Doc/using/configure.rst. * Don't write "all zeros" stats: if _stats_off() and _stats_clear() or _stats_dump() were called. * _PyEval_Fini() now always call _Py_PrintSpecializationStats() which does nothing if stats are all zeros. Co-authored-by: Michael Droettboom <mdboom@gmail.com>
* Add some GC stats to Py_STATS (GH-107581)Mark Shannon2023-08-041-0/+18
|
* gh-106320: Remove private _PyDict C API (#107145)Victor Stinner2023-07-241-3/+4
| | | | | | | | | | | | | Move private _PyDict functions to the internal C API (pycore_dict.h): * _PyDict_Contains_KnownHash() * _PyDict_DebugMallocStats() * _PyDict_DelItemIf() * _PyDict_GetItemWithError() * _PyDict_HasOnlyStringKeys() * _PyDict_MaybeUntrack() * _PyDict_MergeEx() No longer export these functions.
* gh-105927: Remove _PyWeakref_GetWeakrefCount() (#106007)Victor Stinner2023-06-231-0/+1
| | | | | | Remove _PyWeakref_GetWeakrefCount() and _PyWeakref_ClearRef() from the public C API: move them to the internal C API. Refactor also _weakref_getweakrefs() code to make it more readable.
* gh-105268: Add _Py_FROM_GC() function to pycore_gc.h (#105362)Victor Stinner2023-06-061-8/+6
| | | | | | | | | | * gcmodule.c reuses _Py_AS_GC(op) for AS_GC() * Move gcmodule.c FROM_GC() implementation to a new _Py_FROM_GC() static inline function in pycore_gc.h. * _PyObject_IS_GC(): only get the type once * gc_is_finalized(à) and PyObject_GC_IsFinalized() use _PyGC_FINALIZED(), instead of _PyGCHead_FINALIZED(). * Remove _Py_CAST() in pycore_gc.h: this header file is not built with C++.
* gh-99113: Add Py_MOD_PER_INTERPRETER_GIL_SUPPORTED (gh-104205)Eric Snow2023-05-051-0/+1
| | | Here we are doing no more than adding the value for Py_mod_multiple_interpreters and using it for stdlib modules. We will start checking for it in gh-104206 (once PyInterpreterState.ceval.own_gil is added in gh-104204).
* gh-94673: More Per-Interpreter Fields for Builtin Static Types (gh-103912)Eric Snow2023-05-031-28/+3
| | | | | his involves moving tp_dict, tp_bases, and tp_mro to PyInterpreterState, in the same way we did for tp_subclasses. Those three fields are effectively const for builtin static types (unlike tp_subclasses). In theory we only need to make their values immortal, along with their contents. However, that isn't such a simple proposition. (See gh-103823.) In the meantime the simplest solution is to move the fields into the interpreter. One alternative is to statically allocate the values, but that's its own can of worms.
* gh-103743: Add PyUnstable_Object_GC_NewWithExtraData (GH-103744)Jurica Bradarić2023-05-021-0/+13
| | | | Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
* gh-104028: Reduce object creation while calling callback function from gc ↵Dong-hee Na2023-05-011-1/+11
| | | | (gh-104030)
* gh-101408: PyObject_GC_Resize should calculate preheader size. (gh-101741)Dong-hee Na2023-04-231-6/+7
|
* gh-84436: Implement Immortal Objects (gh-19474)Eddie Elizondo2023-04-221-1/+14
| | | | | | | | | This is the implementation of PEP683 Motivation: The PR introduces the ability to immortalize instances in CPython which bypasses reference counting. Tagging objects as immortal allows up to skip certain operations when we know that the object will be around for the entire execution of the runtime. Note that this by itself will bring a performance regression to the runtime due to the extra reference count checks. However, this brings the ability of having truly immutable objects that are useful in other contexts such as immutable data sharing between sub-interpreters.
* gh-102013: Add PyUnstable_GC_VisitObjects (#102014)Jacob Bower2023-03-141-0/+24
|
* GH-101578: Normalize the current exception (GH-101607)Mark Shannon2023-02-081-3/+2
| | | | | | | | | | * Make sure that the current exception is always normalized. * Remove redundant type and traceback fields for the current exception. * Add new API functions: PyErr_GetRaisedException, PyErr_SetRaisedException * Add new API functions: PyException_GetArgs, PyException_SetArgs
* gh-99845: Clean up _PyObject_VAR_SIZE() usage (#99847)Victor Stinner2022-11-291-3/+2
| | | | | | * code_sizeof() now uses an unsigned type (size_t) to compute the result. * Fix _PyObject_ComputedDictPointer(): cast _PyObject_VAR_SIZE() to Py_ssize_t, rather than long: it's a different type on 64-bit Windows. * Clarify that _PyObject_VAR_SIZE() uses an unsigned type (size_t).
* gh-99300: Use Py_NewRef() in Modules/ directory (#99469)Victor Stinner2022-11-141-2/+1
| | | | Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
* gh-97922: Run the GC only on eval breaker (#97920)Pablo Galindo Salgado2022-10-081-3/+24
|
* gh-94673: Recover Weaklist Lookup Performance (gh-95544)Eric Snow2022-08-041-3/+6
| | | gh-95302 seems to have introduced a small performance regression. Here we make some minor changes to recover that lost performance.
* gh-95324: Emit a warning if an object doesn't call PyObject_GC_UnTrack ↵Pablo Galindo Salgado2022-07-271-0/+7
| | | | during deallocation in debug mode (#95325)
* bpo-40514: Drop EXPERIMENTAL_ISOLATED_SUBINTERPRETERS (gh-93185)Eric Snow2022-05-271-8/+0
| | | | | | | This was added for bpo-40514 (gh-84694) to test out a per-interpreter GIL. However, it has since proven unnecessary to keep the experiment in the repo. (It can be done as a branch in a fork like normal.) So here we are removing: * the configure option * the macro * the code enabled by the macro
* gh-92036: Fix gc_fini_untrack() (#92037)Victor Stinner2022-05-041-0/+6
| | | | | | | | Fix a crash in subinterpreters related to the garbage collector. When a subinterpreter is deleted, untrack all objects tracked by its GC. To prevent a crash in deallocator functions expecting objects to be tracked by the GC, leak a strong reference to these objects on purpose, so they are never deleted and their deallocator functions are not called.
* bpo-40170: PyType_SUPPORTS_WEAKREFS() becomes a regular function (GH-30938)Victor Stinner2022-01-271-1/+1
| | | | | | | | Convert the PyType_SUPPORTS_WEAKREFS() macro to a regular function. It no longer access the PyTypeObject.tp_weaklistoffset member directly. Add _PyType_SUPPORTS_WEAKREFS() static inline functions, used internally by Python for best performance.
* bpo-45953: Statically initialize all the non-object PyInterpreterState ↵Eric Snow2022-01-141-15/+11
| | | | | fields we can. (gh-30589) https://bugs.python.org/issue45953
* bpo-46070: _PyGC_Fini() untracks objects (GH-30577)Victor Stinner2022-01-131-0/+24
| | | | | | | Py_EndInterpreter() now explicitly untracks all objects currently tracked by the GC. Previously, if an object was used later by another interpreter, calling PyObject_GC_UnTrack() on the object crashed if the previous or the next object of the PyGC_Head structure became a dangling pointer.
* bpo-45947: Place dict and values pointer at fixed (negative) offset just ↵Mark Shannon2021-12-071-35/+29
| | | | | | | | | | | | | | | | | | | before GC header. (GH-29879) * Place __dict__ immediately before GC header for plain Python objects. * Fix up lazy dict creation logic to use managed dict pointers. * Manage values pointer, placing them directly before managed dict pointers. * Convert hint-based load/store attr specialization target managed dict classes. * Specialize LOAD_METHOD for managed dict objects. * Remove unsafe _PyObject_GC_Calloc function. * Remove unsafe _PyObject_GC_Malloc() function. * Add comment explaning use of Py_TPFLAGS_MANAGED_DICT.
* bpo-45786: Allocate space for frame in frame object. (GH-29729)Mark Shannon2021-11-291-1/+0
|
* bpo-41710: gc_collect_main() uses _PyTime_GetPerfCounter() (GH-28676)Victor Stinner2021-10-011-2/+2
| | | | | | | | | If the DEBUG_STATS debug flag is set, gc_collect_main() now uses _PyTime_GetPerfCounter() instead of _PyTime_GetMonotonicClock() to measure the elapsed time. On Windows, _PyTime_GetMonotonicClock() only has a resolution of 15.6 ms, whereas _PyTime_GetPerfCounter() is closer to a resolution of 100 ns.
* bpo-41117: Cleanup subtract_refs() (GH-27593)Victor Stinner2021-08-041-3/+3
| | | | | | subtract_refs() reuses the 'op' variable rather than calling the FROM_GC() macro twice. Issue reported by William Pickard.
* bpo-28254: Add a C-API for controlling the GC state (GH-25687)scoder2021-04-281-6/+29
| | | | | | | | Add new C-API functions to control the state of the garbage collector: PyGC_Enable(), PyGC_Disable(), PyGC_IsEnabled(), corresponding to the functions in the gc module. Co-authored-by: Pablo Galindo <Pablogsal@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
* bpo-43439: Wrapt the tuple in the audit events for the gc module (GH-24836)Pablo Galindo2021-03-141-2/+2
|
* bpo-43439: Add audit hooks for gc functions (GH-24794)Pablo Galindo2021-03-101-0/+11
|
* bpo-43268: Pass interp rather than tstate to internal functions (GH-24580)Victor Stinner2021-02-191-15/+15
| | | | | | | | | | | | | | | Pass the current interpreter (interp) rather than the current Python thread state (tstate) to internal functions which only use the interpreter. Modified functions: * _PyXXX_Fini() and _PyXXX_ClearFreeList() functions * _PyEval_SignalAsyncExc(), make_pending_calls() * _PySys_GetObject(), sys_set_object(), sys_set_object_id(), sys_set_object_str() * should_audit(), set_flags_from_config(), make_flags() * _PyAtExit_Call() * init_stdio_encoding() * etc.
* bpo-35134, Include: Move pytime.h to cpython/pytime.h (GH-23988)Nicholas Sim2021-02-161-1/+0
| | | | This change is backward compatible since C extension modules must not include "pytime.h" directly, but only include "Python.h".
* bpo-42519: Replace PyObject_MALLOC() with PyObject_Malloc() (GH-23587)Victor Stinner2020-12-011-2/+2
| | | | | | | | | No longer use deprecated aliases to functions: * Replace PyObject_MALLOC() with PyObject_Malloc() * Replace PyObject_REALLOC() with PyObject_Realloc() * Replace PyObject_FREE() with PyObject_Free() * Replace PyObject_Del() with PyObject_Free() * Replace PyObject_DEL() with PyObject_Free()