KurimuzonAkuma / pyrogram

Elegant, modern and asynchronous Telegram MTProto API framework in Python for users and bots
https://pyrogram.org
GNU Lesser General Public License v3.0
358 stars 96 forks source link

network layer #95

Open deus-developer opened 2 months ago

Danstiv commented 4 weeks ago

Hello @deus-developer! I'm not a pyrogram developer but am interested in some changes and you've made some of them. For example, I want to refactor the dispatcher, which is what you did in this PR. In your version of the dispatcher, the parser search logic has been changed, previously a dictionary was used, and you perform search using isinstance. I did some experimenting and found that this approach was noticeably slower than dictionary lookups, and since this is a key element of the library, I think performance is important here.

So, finding the parser can take more than a microsecond. Instead of a fixed 25 nanoseconds.

Probably it's not a big performance hit, but other than that, 15 elifs in a row doesn't look very good, and it might be worth going back to using a dictionary.

@KurimuzonAkuma code example

Danstiv commented 3 weeks ago

I checked the difference between calling synchronous and asynchronous functions, as expected, asynchronous ones are called slower, the difference is 2.8 times (18 ns vs 50 ns on my machine). But such a call is performed once when parsing update, so I think that we can degrade performance by 30 ns in some cases and make all parsing methods asynchronous, even if there is only synchronous code inside. And make them classmethods by the way, why are they static?..