-
Notifications
You must be signed in to change notification settings - Fork 50
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
Performing optimizations in the presence of Py_DECREF()
#582
Comments
What's a safe dealloc though? Freeing a string or int, sure. But freeing a tuple has to ask the question recursively, because the tuple might contain something with a refcount equal to 1 whose dealloc is not safe; same for all containers (e.g. essentially all class instances). |
Freeing a tuple is safe because it only calls |
So the only unsafe deallocs are ones that can directly invoke Python code (not via |
...or C extension types with |
Couldn't weakrefs be separately put on a queue that's processed later? |
Probably, yeah. |
Anything that's not "safe" goes on a queue for later. |
Looking at the new stats for micro-ops, it looks like the micro-ops that do not contain So this may not be so urgent, although still worth doing. |
We want to be able to optimize larger regions of code than a single instruction, but we can't (at least not as effectively) if arbitrary code can run in the middle of region.
Py_DECREF()
can run arbitrary code.We should change
Py_DECREF()
so that it cannot run arbitrary code. CurrentlyPy_DECREF(op)
calls_Py_Dealloc(op)
which callsPy_TYPE(op)->tp_dealloc(op)
.We can modify
_Py_Dealloc(op)
to check a flag to see if there is a safe dealloc. If the dealloc is safe then call it, otherwise add the object to a queue to be deallocated later. Presumably on the eval breaker check.This is going to slow down
_Py_Dealloc
, but hopefully not by much, and certainly less than the speedups that will be enabled.An alternative approach is to make all the decrefs explicit in the IR and move them to the end of the superblock. This is IMO more complex and also prevents carrying optimizations across superblock boundaries.
Original discussion: #402
The text was updated successfully, but these errors were encountered: