harvimt / quamash

Implementation of the PEP 3156 event-loop (asyncio) api using the Qt Event-Loop
BSD 2-Clause "Simplified" License
264 stars 46 forks source link

Format expensive debug strings only when they're used #84

Closed erdewit closed 6 years ago

erdewit commented 6 years ago

Some profiling of a custom benchmark that I use for distex shows that quamash spends most of its time in formatting debug messages, even when they're not used. This PR makes the formatting conditional on the debug setting of the loop. The timerEvent() method has also been streamlined a bit.

The benchmark goes from 7000 reps/s to over 14000 with these changes. For comparison, the standard asyncio loop does 31000 reps/s and uvloop up to 50000.

harvimt commented 6 years ago

This seems like a good idea, I'm not entirely sure what the debug flag is even for if it's not for this.

That being said I've thought about migrating everything to the brace style adapter pattern which would then only format messages that are actually printed, (so if the loglevel is set to ERROR even info messages don't use much).

OTOH it's adding 2 levels of indirection (the style adapter and the BraceMessage class), so I figured it might very well be slower, so I went with YAGNI.

Additionally there's f-strings... which can't be made lazy (without lots of hacks), but format much faster (since the format string is parsed at compile time, among other reasons.). Also would have to drop support for python 3.5... which maybe isn't a big deal... but I'm not sure it would be worth it.

Though... comparing all these things (especially if you using something really exotic, like macropy), might make a good blog post.