Yeah, I can help with that!
What iscProfile?
cProfileis the built-inperformance profilerin Python. It tracks every function call made during a program's execution and records:
number of calls,
total time spent in each function,
time per call, etc.
number of calls,
total time spent in each function,
time per call, etc.
It's super useful for spotting bottlenecks in pure Python code. You usually run it like:
────────────────────────────────────────────────────────────────────
bashCopypython -m cProfile myscript.py
Or programmatically:
────────────────────────────────────────────────────────────────────
pythonCopyimport cProfile
def myfunc():
...
cProfile.run("myfunc()")
So what’s the issue here?
The original question was:
Are you aware of issues where calling a method on a class exported from a pybind11 extension under cProfile messes up the profile in weird ways under Python 3.12?
This suggests that under Python 3.12,cProfile is failing to record some Python function calls—butonly when those calls involve a method exported from a pybind11 C++ extension.
More specifically, noted:
"the missing calls in the profile are Python calls higher in the stackbeforethe pybind11 call happens"
That's strange. It's like cProfile istruncating or skippingpart of the Python call stack when a pybind11 method is called.
Possible directions to investigate:
Python 3.12 changes:Python 3.12 introduced a lot of changes to the interpreter internals, including optimizations in frame evaluation (PEP 659,PEP 523hooks, etc.).If pybind11 doesn’t cooperate cleanly with these changes, it could mess with the profiler.
Python 3.12 introduced a lot of changes to the interpreter internals, including optimizations in frame evaluation (PEP 659,PEP 523hooks, etc.).
If pybind11 doesn’t cooperate cleanly with these changes, it could mess with the profiler.
pybind11 behavior:pybind11 generates trampoline functions that get invoked from Python. If the lambda is inlined or not instrumented properly, cProfile might just "skip" over it.If the transition to native code is not properly tracked, then the profiler might not resume correctly after the call.
pybind11 generates trampoline functions that get invoked from Python. If the lambda is inlined or not instrumented properly, cProfile might just "skip" over it.
If the transition to native code is not properly tracked, then the profiler might not resume correctly after the call.
Profiler hooks and compiled code:cProfile is written in C and relies on Python's internal frame evaluation hooks.Calling C++ code via pybind11 bypasses Python bytecode execution, so the profiler may not “see” into the part, but itshouldstill see everything leading up to it.
cProfile is written in C and relies on Python's internal frame evaluation hooks.
Calling C++ code via pybind11 bypasses Python bytecode execution, so the profiler may not “see” into the part, but itshouldstill see everything leading up to it.
Python 3.12 changes:
Python 3.12 introduced a lot of changes to the interpreter internals, including optimizations in frame evaluation (PEP 659,PEP 523hooks, etc.).
If pybind11 doesn’t cooperate cleanly with these changes, it could mess with the profiler.
Python 3.12 introduced a lot of changes to the interpreter internals, including optimizations in frame evaluation (PEP 659,PEP 523hooks, etc.).
If pybind11 doesn’t cooperate cleanly with these changes, it could mess with the profiler.
pybind11 behavior:
pybind11 generates trampoline functions that get invoked from Python. If the lambda is inlined or not instrumented properly, cProfile might just "skip" over it.
If the transition to native code is not properly tracked, then the profiler might not resume correctly after the call.
pybind11 generates trampoline functions that get invoked from Python. If the lambda is inlined or not instrumented properly, cProfile might just "skip" over it.
If the transition to native code is not properly tracked, then the profiler might not resume correctly after the call.
Profiler hooks and compiled code:
cProfile is written in C and relies on Python's internal frame evaluation hooks.
Calling C++ code via pybind11 bypasses Python bytecode execution, so the profiler may not “see” into the part, but itshouldstill see everything leading up to it.
cProfile is written in C and relies on Python's internal frame evaluation hooks.
Calling C++ code via pybind11 bypasses Python bytecode execution, so the profiler may not “see” into the part, but itshouldstill see everything leading up to it.
Recommendations
Try on Python 3.11 or 3.10to confirm it's a Python 3.12 regression.
Usesys.setprofile()to set a manual profile hook—see if it gets the same behavior.
Try a more direct profilerlikePy-SpyorLinuxperf(Ralf mentioned this).
Consider opening an issue on thepybind11orCPythonbug trackers if you can make a minimal reproducible example.
Try on Python 3.11 or 3.10to confirm it's a Python 3.12 regression.
Usesys.setprofile()to set a manual profile hook—see if it gets the same behavior.
Try a more direct profilerlikePy-SpyorLinuxperf(Ralf mentioned this).
Consider opening an issue on thepybind11orCPythonbug trackers if you can make a minimal reproducible example.
If you want, I can help you write or debug such a repro case. Just let me know what you've tried or what you want it to test.