Skip to content
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

PM managed GC #6554

Merged
merged 8 commits into from
Dec 15, 2024
Merged

PM managed GC #6554

merged 8 commits into from
Dec 15, 2024

Conversation

dktapps
Copy link
Member

@dktapps dktapps commented Dec 2, 2024

This is a first look at #5628.

In a nutshell: Server MemoryManager now controls when GC is called. The mechanism by which it's activated remains the same as PHP's own, but instead of being called in random code paths & causing huge lag spikes, it gets called at a predictable location & can therefore be tracked in timings reports.

This required re-implementing PHP's garbage collection threshold algorithm, as it's not possible to reliably make PHP update its own internal threshold when GC isn't always enabled.

A couple of considerations I came across while working on this. It seems like PHP's GC algorithm is rather suboptimal for PM's use case.

  • The average GC run in PM takes about 10-15ms. To my surprise, the biggest factor influencing performance is actually the depth of referenced objects. This means that anything referencing Server getting unref'd causes a hit to GC performance because Server will land in the GC root buffer, and Server is a gateway to everything.
  • Long-lived complex objects like Server will frequently end up taking up space in the GC root buffer (whenever their refcounts go down), and then the GC wastes a ton of CPU time trying to scan them for dependencies. There's no real solution for this beyond requesting PHP to implement generations in the GC (A way to stop GC wasting CPU time on long-lived objects (GC generations or something else) php/php-src#17131).
  • Acyclic objects flood the root buffer and cause the threshold to go sky-high. In itself, this doesn't affect the GC's performance that much (e.g. a Vector3 takes only tens of nanoseconds per object to dispose), but the filling up of the GC root buffer causes more frequent runs, causing complex objects to get repeatedly scanned as in the above point.

Follow-up

  • Explore how much performance benefit we really get from the World's block cache. These cached objects tend to flood the GC root buffer, causing collection sweeps that collect barely any objects.
  • After re-implementing the PHP native threshold algorithm, I realized it probably wasn't optimal for PM's use case. For example, the default root buffer size should probably be significantly larger (around 100k or so) to allow for the World block cache to not constantly trigger GC.

@dktapps dktapps added Category: Core Related to internal functionality Status: Insufficiently Tested Type: Enhancement Contributes features or other improvements to PocketMine-MP Performance labels Dec 2, 2024
@dktapps dktapps requested a review from Muqsit December 2, 2024 21:01
@dktapps dktapps requested a review from a team as a code owner December 2, 2024 21:01
pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Dec 2, 2024
pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Dec 2, 2024
pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Dec 2, 2024
pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Dec 2, 2024
GC threshold recalculation is supposed to use the root count _after_ GC, not before (presumably so it doesn't count non-cyclic objects).
@dktapps
Copy link
Member Author

dktapps commented Dec 12, 2024

Related issues:

php/php-src#17127 (Acyclic objects landing in GC root buffer)
php/php-src#17131 (No generational GC to avoid wasting CPU time on long-lived objects)

pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Dec 13, 2024
pmmp-admin-bot[bot]
pmmp-admin-bot bot previously approved these changes Dec 15, 2024
I theorized that the number of roots removed should be a better heuristic than cycles, but in practice it caused GC to run a lot more often.

The cost of expensive objects like Server is parasitic, so the less we run GC, the more the cost is amortized.
There are ways we can eliminate Server's cost from GC, but that's a task for another time.
@dktapps dktapps merged commit 8f8fe94 into minor-next Dec 15, 2024
30 checks passed
@dktapps dktapps deleted the pm-managed-gc branch December 15, 2024 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Core Related to internal functionality Performance Status: Insufficiently Tested Type: Enhancement Contributes features or other improvements to PocketMine-MP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant