| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
This implements a mark and sweep GC for the free-threaded builds of CPython. The implementation relies on mimalloc to find GC tracked objects (i.e., "containers").
|
@DinoV, @pablogsal, @nascheme - I think this is ready for review now. There is duplicate code between Python/gc.c and Python/gc_free_threading.c for now. I'd like to avoid any substantial refactoring, while @markshannon's incremental GC PR is still outstanding, even at the cost of some duplicate code. I'll put up a PR for the devguide as well. |
Sorry, something went wrong.
|
The design, structure and code style look good to me. A couple of minor suggestions. Next to the definition of ob_tid, I think it should also mention that this field is (ab)used for the linked-list and for the GC refs count. Instead of from_block() I'd prefer a bit more descriptive name, e.g. op_from_block(). I think using the marking stack is okay, despite the extra allocations needed. If it is turns out to be a problem, e.g. a program with a very deeply linked object structure, a fairly simple fix is as follows. Limit the size of the stack. If the size is exceeded, re-start the marking process. An additional refinement is to have a "already marked" bit on each mimalloc page and that way if you re-start, those pages can be quickly skipped. Note that the PyGC_Head info cannot be removed unless a different mechanism for the "trashcan" is made. It could use the _PyObjectStack structure as well. |
Sorry, something went wrong.
|
Thanks for the review Neil. I've renamed from_block to op_from_block and added a comment to the ob_tid definition.
Yeah, that makes sense.
In a subsequent change (not yet a PR), I'm using ob_tid for the trashcan: colesbury@9e06349, which avoids having to worry about allocation failures. I expect to use _PyObjectStack for the (not yet implemented) biased reference counting an inter-thread queue, where ob_tid cannot be (ab)used as a linked list pointer. |
Sorry, something went wrong.
| } | ||
|
|
||
| static int | ||
| gc_visit_heaps_locked(PyInterpreterState *interp, mi_block_visit_fun *visitor, |
There was a problem hiding this comment.
Do we want to call this gc_visit_heaps_lock_held per our discussion on naming these style of functions?
Sorry, something went wrong.
| if (_PyObject_GC_IS_TRACKED(op) && !_Py_IsImmortal(op)) { | ||
| // If update_refs hasn't reached this object yet, mark it | ||
| // as (tentatively) unreachable and initialize ob_tid to zero. | ||
| if (!gc_is_unreachable(op)) { |
There was a problem hiding this comment.
It'd be nice to factor this into a helper like gc_init_refs or gc_maybe_init_refs that is used here and in update_refs
Sorry, something went wrong.
| if (PyTuple_CheckExact(op)) { | ||
| _PyTuple_MaybeUntrack(op); | ||
| if (!_PyObject_GC_IS_TRACKED(op)) { | ||
| if (gc_is_unreachable(op)) { |
There was a problem hiding this comment.
This could similarly be a gc_restore_refs or gc_maybe_restore_refs and used below
Sorry, something went wrong.
| return NULL; | ||
| } | ||
|
|
||
| // Append all objects to a worklist. This abuses ob_tid. We will restore |
There was a problem hiding this comment.
Is it worth stopping the world here? It seems like we're fine to race with ob_tid from a correctness stand point, but it seems like a lot of objects are going to end up with merged ref counts in a multithreaded environment!
Sorry, something went wrong.
There was a problem hiding this comment.
We need to stop the world for the heap traversal (gc_visit_heaps) to be thread-safe... which I forgot to add here. I'll also add an assert to gc_visit_heaps() that the world is stopped.
This doesn't merge most refcounts. They get restored from the mimalloc data structure. They'll only get merged if the owning thread has already exited.
Sorry, something went wrong.
| // it later. NOTE: We can't append to the list during gc_visit_heaps() | ||
| // because PyList_Append() may reclaim an abandoned mimalloc segment | ||
| // while we are traversing it. | ||
| struct get_objects_args args = { 0 }; |
There was a problem hiding this comment.
Ditto to get referrers
Sorry, something went wrong.
|
|
||
| op->ob_tid = 0; | ||
| op->ob_ref_local = 0; | ||
| op->ob_ref_shared = _Py_REF_SHARED(refcount, _Py_REF_MERGED); |
There was a problem hiding this comment.
It seems like this needs to be an atomic operation? Can this just be a call to _Py_ExplicitMergeRefcount?
Sorry, something went wrong.
There was a problem hiding this comment.
The other threads in the interpreter must be paused, so no atomics necessary. I'll add an assert here.
We could probably use _Py_ExplicitMergeRefcount(), but it seems convenient to avoid the atomic operations.
Sorry, something went wrong.
|
|
||
| // Clear weakrefs and enqueue callbacks (but do not call them). | ||
| clear_weakrefs(state); | ||
| _PyEval_StartTheWorld(interp); |
There was a problem hiding this comment.
So I think if I understand this correctly what's going to happen with the thread ID and any reference count changes that may happen at this point, but the assumption is that the thread ID is never going to actually clash with an object reference in the work list. And then anything which does have a reference count operation will just become merged when we restore it?
Sorry, something went wrong.
There was a problem hiding this comment.
Yeah, the key assumption is that _PyThread_Id() is itself a pointer to a distinct object so never conflicts with an object in the worklists. The implementation of _PyThread_Id() uses the address of the platform's thread control block or equivalent.
On the GC side, we abuse ob_tid for two purposes:
Some worklists are only used while other threads are paused (e.g., in _PyGC_GetObjects()). Other worklists are created when threads are paused, but still used while other threads may be running (e.g., unreachable, wrcb_to_call).
If the worklist continues to be used while other threads are running, then there are two other important considerations:
Sorry, something went wrong.
|
Here's the PR so far for the devguide: python/devguide#1263 |
Sorry, something went wrong.
* pythongh-112529: Implement GC for free-threaded builds This implements a mark and sweep GC for the free-threaded builds of CPython. The implementation relies on mimalloc to find GC tracked objects (i.e., "containers").
* pythongh-112529: Implement GC for free-threaded builds This implements a mark and sweep GC for the free-threaded builds of CPython. The implementation relies on mimalloc to find GC tracked objects (i.e., "containers").
| Back | FazBrowse Home | New Git URL |
This implements the GC for free-threaded builds. The free-threading GC follows the same basic algorithms as the existing GC, but operates on different data types. Specifically:
There is a bunch of clean-up and improvements that I'd like to defer to later PRs to keep this size of this manageable: