Improve Sanic performance


I want to discuss Sanic performance improvements. Since we don’t support Python versions lower than 3.6

In this case, I think it would be cool to think about code optimization, and at this time we will increase our performance in the long term.

For example, in Sanic we have been using format function in a lot of places. Since Python 3.6 has introduced nice f-strings formatting also it works faster than the standard format function.

And I assume that it would be a nice first step in order to improve Sanic performance.

For example, steps which will be needed(if we decide to do it)

  1. Replace all format functions to f-strings where it is possible, and my suggestion is - start this improvement from tests package.

  2. Then replace all format functions in other modules(app, router and etc)

  3. Also optimize code where it is possible, for example, to use(list comprehension instead of some for...loop iteration and etc)

And please, don’t take it as an urgent action;) I’ll be happy to hear your thoughts and etc.

Also, if it sounds good to you,I’d be happy to work on it, and it will be a nice chance to explore Sanic’s source;)


1 Like

Makes sense in the f strings. Especially because they are also just easier to look at.

As for comprehension and loops, I’d take a more cautious approach. If there is a performance benefit, then great. But if it’s minimal, I think there’s a question about readability and maintainability that needs to be balanced. I think it’s a case by case scenario as to which is better given the context.

If you want to make a PR on f strings, great! It is something I was planning on doing.

1 Like

Yes, I totally agree here.

Yes, I’ll be happy to work on it! and I plan to start from tests package.

Should we create an issue on Github in order to track work?

Nah… I think you can make the PR and link back here, and just continue the conversation on the PR if needed.

Got it, cool! I’ll make a PR ASAP.

@ahopkins Sorry for the extremly delay, I’ll make PR in few days.

1 Like