logoalt Hacker News

Python: Tprof, a Targeting Profiler

74 pointsby jonatrontoday at 9:00 AM6 commentsview on HN

Comments

stabblestoday at 4:26 PM

`sys.monitoring` is nice. I used it to find inefficiently ordered chains of branches in one of my projects. For example a chain of `if isinstance(foo, ...); elif isinstance(foo, ...); elif isinstance(foo, ...);` can be reordered from most to least popular based on a representative run, to avoid evaluating branch conditions more than necessary. You collect BRANCH_LEFT/BRANCH_RIGHT events, divide the code objects into "basic blocks", build a graph from them with edges weighted by frequency, and identify chains using a simple algorithm [1]. Then report chains where the long jump is taken more often than the short jump. It's like semi-automatic PGO for Python.

[1]: https://dl.acm.org/doi/abs/10.1145/93542.93550

benruttertoday at 5:07 PM

This looke incredibly helpful! Not sure if I hust haven't come across the right tools yet, but I always find performance monitoring in python very tricky.

There aren't any tools I know of, that meet the standard of say coverage, where they're easy to use for beginners and meet 90% of use cases.

jesse__today at 5:15 PM

I remember looking at python profiling tools a couple years ago and being pretty disappointed. Most added a huge amount of runtime noise, the lowest being something like +50% runtime, which to me is completely unacceptable. The profiler I wrote and use every day for a side project adds well less than 1% runtime overhead.

I wonder what the overhead of sys.monitoring is. It is possible to instrument functions like this without using that API.. you can just walk the AST at startup and do it yourself. Would be interesting to see a very minimal instrumenting profiler that did that and compare the two.

I also love the 'targeted'/instrumenting profiling API. I think sampling profilers are good at scratching the surface, but quickly run out of useful information when getting down to the goods. Happy to see people doing instrumenting profilers.

Thanks for sharing :)

show 2 replies