[pypy-dev] performance issue with context managers

Armin Rigo arigo at tunes.org
Thu May 16 09:42:39 CEST 2013


Hi Jasper,

On Thu, May 16, 2013 at 9:14 AM, Jasper Spaans <spaans at fox-it.com> wrote:
> I was toying around a bit with writing a statistical profiler in python,
> and came up with https://gist.github.com/jap/5584946

It's a statistical profiler based on signals: whenever a signal is
delivered, it checks where it is and counts.  What occurs is that the
signal delivery points are a bit more restricted when running JITted
code.  The inner loop of your example:

>         for t in range(100000):
>             with ProfilerContext("c2"):
>                 s = s + "a"

is quickly compiled to machine code that does this:

    guard that t < 1000000
    append "c2" to the list local_context_stack.data
    s = s + "a"
    remove the last item from local_context_stack.data
    guard that there was no signal
    jump back to the top of the loop

So it only checks for signals once per loop at the end, instead of (as
usual when interpreting) at random points during the loop.  Signals
will never be delivered when "c2" is in the local_context_stack...


A bientôt,

Armin.


More information about the pypy-dev mailing list