Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

GH-126491: Lower heap size limit with faster marking #127519

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

Merged
merged 24 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3038a78
Faster marking of reachable objects
markshannon Nov 9, 2024
c024484
Handle more classes in fast marking
markshannon Nov 10, 2024
e8497ae
Add support for asyn generators on fast path. Simplify counting
markshannon Nov 11, 2024
4c1a6bc
Check stackref before converting to PyObject *
markshannon Nov 11, 2024
6efb4c0
Rename stuff
markshannon Nov 13, 2024
b1c7ab0
Remove expand_region_transitively_reachable and use move_all_transiti…
markshannon Nov 13, 2024
07f228b
Merge branch 'main' into faster-marking
markshannon Dec 2, 2024
51ff78e
Fix compiler warnings and linkage
markshannon Dec 2, 2024
df907b5
Fix another linkage issue
markshannon Dec 2, 2024
9ca64f5
Try 'extern'
markshannon Dec 2, 2024
bda13f4
Go back to PyAPI_FUNC and move functions together
markshannon Dec 2, 2024
d9d63c8
Use _Py_FALLTHROUGH
markshannon Dec 2, 2024
57b8820
Add necessary #ifndef Py_GIL_DISABLED
markshannon Dec 2, 2024
a607059
Go back to using tp_traverse, but make traversal more efficient
markshannon Dec 3, 2024
1545508
Tidy up
markshannon Dec 3, 2024
a1a38c8
A bit more tidying up
markshannon Dec 3, 2024
68fc90b
Move all work to do calculations to one place
markshannon Dec 3, 2024
8893cf5
Assume that increments are 50% garbage for work done calculation
markshannon Dec 3, 2024
ba20c7c
Elaborate comment
markshannon Dec 4, 2024
8262bf0
More tweaking of thresholds
markshannon Dec 4, 2024
3c2116e
Do some algebra
markshannon Dec 4, 2024
72d0284
Revert to 2M+I from 3M+I
markshannon Dec 4, 2024
0f182e2
Address review comments
markshannon Dec 5, 2024
d3c21bb
Address review comments and clarify code a bit
markshannon Dec 5, 2024
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
Prev Previous commit
Address review comments and clarify code a bit
  • Loading branch information
markshannon committed Dec 5, 2024
commit d3c21bb57092428592b86af28246ed1221b002f5
10 changes: 5 additions & 5 deletions 10 InternalDocs/garbage_collector.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,15 @@ but doing too much work slows down execution.

To work out how much work we need to do, consider a heap with `L` live objects
and `G0` garbage objects at the start of a full scavenge and `G1` garbage objects
at the end of the scavenge. We don't want amount of garbage to grow, `G1 ≤ G0`, and
at the end of the scavenge. We don't want the amount of garbage to grow, `G1 ≤ G0`, and
we don't want too much garbage (say 1/3 of the heap maximum), `G0 ≤ L/2`.
For each full scavenge we must visit all objects, `T == L + G0 + G1`, during which
`G1` garbage objects are created.

The number of new objects created `N` must be at least the new garbage created, `N ≥ G1`,
assuming that the number of live objects remains roughly constant.
If we set `T == 4*N` we get `T > 4*G1` and `T = L + G0 + G1` => `L + G0 > 3G1`
For a steady state heap `G0 == G1` we get `L > 2G` and the desired garbage ratio.
For a steady state heap (`G0 == G1`) we get `L > 2G0` and the desired garbage ratio.

In other words, to keep the garbage fraction to 1/3 or less we need to visit
4 times as many objects as are newly created.
Expand All @@ -544,11 +544,11 @@ Everything in `M` is live, so `I ≥ G0` and in practice `I` is closer to `G0 +

If we choose the amount of work done such that `2*M + I == 6N` then we can do
less work in most cases, but are still guaranteed to keep up.
Since `I G0 + G1` (not strictly true, but close enough)
`T == M + I == (6N + I)/2` and `(6N + I)/2 4G`, so we can keep up.
Since `I G0 + G1` (not strictly true, but close enough)
`T == M + I == (6N + I)/2` and `(6N + I)/2 4G`, so we can keep up.

The reason that this improves performance is that `M` is usually much larger
than `I` Suppose `M == 10I`, then `T ≅ 3N`.
than `I`. If `M == 10I`, then `T ≅ 3N`.

Finally, instead of using a fixed multiple of 8, we gradually increase it as the
heap grows. This avoids wasting work for small heaps and during startup.
Expand Down
30 changes: 11 additions & 19 deletions 30 Python/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1544,40 +1544,32 @@ mark_at_start(PyThreadState *tstate)
return objects_marked;
}


/* See InternalDocs/garbage_collector.md for more details. */
#define MAX_HEAP_PORTION_MULTIPLIER 5
#define MARKING_PROGRESS_MULTIPLIER 2

static intptr_t
assess_work_to_do(GCState *gcstate)
{
/* The amount of work we want to do depends on two things.
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth linking to the doc from here?

* 1. The number of new objects created
* 2. The heap size (up to twice the number of new objects, to avoid quadratic effects)
* 3. The amount of garbage.
*
* We cannot know how much of the heap is garbage, but we know that no reachable object
* is garbage. We make a (fairly pessismistic) assumption that half the heap not
* reachable from the roots is garbage, and count collections of increments as half as efficient
* as processing the heap as the marking phase.
*
* For a large, steady state heap, the amount of work to do is at least three times the
* number of new objects added to the heap. This ensures that we stay ahead in the
* worst case of all new objects being garbage.
* 2. The heap size (up to a multiple of the number of new objects, to avoid quadratic effects)
*/
intptr_t scale_factor = gcstate->old[0].threshold;
if (scale_factor < 2) {
scale_factor = 2;
}
intptr_t new_objects = gcstate->young.count;
intptr_t max_heap_fraction = new_objects*5;
intptr_t heap_fraction = gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor;
if (heap_fraction > max_heap_fraction) {
heap_fraction = max_heap_fraction;
intptr_t max_heap_portion = new_objects * MAX_HEAP_PORTION_MULTIPLIER;
intptr_t heap_portion = gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor;
if (heap_portion > max_heap_portion) {
heap_portion = max_heap_portion;
}
gcstate->young.count = 0;
return new_objects + heap_fraction;
return new_objects + heap_portion;
}

/* See Internal GC docs for explanation */
#define MARKING_PROGRESS_MULTIPLIER 2

static void
gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats)
{
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.