-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
py/gc: Instrument finaliser code for instances of finaliser encountering AT_MARK. #18013
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
base: master
Are you sure you want to change the base?
Conversation
As can be seen from this branch's test failures, this instrumentation gets triggered only in the VFS tests --- I suspect the commonality here is that this is the only other use of |
The fact that this branch isn't triggered in any portion of the codebase except VFS, also supports the removal of this condition --- it's a pure optimisation that attempts to avoid a potentially-expensive object attribute lookup, and it's never one that actually fails in any cases other than the ones it's causing bugs in. |
This check is a remnant of the original finaliser implementation from 4f7e9f5, all the way from pre-1.0. This was a valid optimisation back then, but after its ordering with other finaliser code changed in 8a2ff2c the invariant it exploits is no longer valid, and it breaks down completely in the case of variable-length allocations made with `mp_obj_malloc_var_with_finaliser`. This resulted in a latent bug in the VFS system, where depending on the precise memory layout, VFS finaliser routines could sometimes be inadvertently missed. This bug was discovered when encountering an identical issue with an attempt to implement __del__ for user classes. As can be seen from the test runs with the instrumentation in micropython#18013, this branch is also never actually hit by normal code outside of VFS. Therefore, whatever the original intent and whether or not it was ever, it's not actually an optimisation now; removing this condition is a strict improvement to garbage collector code size and performance. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
This check is a remnant of the original finaliser implementation from 4f7e9f5, all the way from pre-1.0. This was a valid optimisation back then, but after its ordering with other finaliser code changed in 8a2ff2c the invariant it exploits is no longer valid, and it breaks down completely in the case of variable-length allocations made with `mp_obj_malloc_var_with_finaliser`. This resulted in a latent bug in the VFS system, where depending on the precise memory layout, VFS finaliser routines could sometimes be inadvertently missed. This bug was discovered when encountering an identical issue with an attempt to implement `__del__` for user classes in micropython#18005. As can be seen from the test runs with the instrumentation in micropython#18013, this branch is also never actually hit by normal code outside of VFS. Therefore, whatever the original intent and whether or not it was ever, it's not actually an optimisation now; removing this condition is a strict improvement to garbage collector code size and performance. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2a5d5c5
to
7e1f133
Compare
Code size report:
|
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Doing a quick test, I suggest making a function |
The garbage collector is changing it to
I've got similar thoughts --- what I really need is better tooling here. I'm currently working on a GDB plugin that should hopefully make it a bit easier to see what's going on by generating graphviz data-structure graphs of MicroPython's heap. |
Submitting as a PR for the sake of CI and easy reference to the results --- this SHOULD NOT be merged.
Part of research into some layout-sensitive inconsistent GC finaliser behavior I discovered in #18005.
This PR adds instrumentation to this branch to detect and report any other cases in the test suite that trigger the particular circumstances that trigger this bug.