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
_PyFunction_FastCallDict and _PyFunction_FastCallKeywords: fast path not used #76016
Comments
Just a minor performance issue. The C functions _PyFunction_FastCallDict() and _PyFunction_FastCallKeywords() (branch 'master', Objects/call.c) and their predecessors fast_function() and _PyFunction_FastCallDict() in Python/ceval.c all contain the following sub-expression in the "if"-statement for the fast-path. For instance Objects/call.c:318 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE) Now, if co_flags has any of the CO_FUTURE_... bits set, the expression is always False and the fast path is not used. Currently this affects only Python 3.6 and Python 2.7, because other Python versions do not use the __future__ mechanism. The fix is simple. Replace the faulty sub-expression by (co->co_flags & (~PyCF_MASK)) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) I discovered this issue while debugging reference leaks in Stackless Python a few month ago. It is hard to write a test case, but one can compare C call stacks using a debugger. $ ulimit -c unlimited # enable core dumps
$ python3.6 -c 'from __future__ import generator_stop; import os; (lambda: os.abort())()'
$ gdb -batch -ex bt python3.6 core > trace_with_future
$ python3.6 -c 'import os; (lambda: os.abort())()'
$ gdb -batch -ex bt python3.6 core > trace_without_future If you compare the traces, the difference is in stack frame #9. Same for python2.7. |
I proposed PR 4087 to implement this optimization. I wouldn't call it a "fix", since the "co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)" check exists since Python 2.7 at least (whereas Python 2.7 also has CO_FUTURE_xxx flags).
I prefer to call it a performance opportunity :-) |
I reset Versions to Python 3.7. I don't consider this issue as a bug, but only as a new optimization. So it can only go into the future Python 3.7. |
Thank you Anselm Kruis for spotting this nice optimization opportunity! Sadly, as I wrote, I don't want to backport the optimization to the stable Python 3.6 branch. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: