⚡️ Improve FastAPI performance by +265% 🚀🚀
These performance improvement tips are from Marcelo Trylesinski, the maintainer of FastAPI. These insights are shared in a LinkedIn post, which you can find here
10 tips for performances improvement :
- use uvloop +10%🚀, is a fast, drop-in replacement of the built-in asyncio event loop
- use httptools - faster parsing +10%🚀, Python binding for nodejs HTTP parser, you just need to have it installed in your environment
- use bigger threadpool +5%🚀
- use simple async application, +15%🚀, no overhead of threads
- remove duplicate validation, have only FastAPI do the validation +25%🚀
- use ORJSON + 5% 🚀(if not using the latest Pydanctic version already)
- consider running without validation is an option, 150%, without logging +25%. Both not a general recommendation!
- use the most recent version of FastAPI, + 265% 🚀- this is related to pydantic going rust
- use ASGI Middleware: see https://lnkd.in/eUW5-bwG
- compress responses
Stay tuned for the recording of the conference, which will be released on the EuroPython channel in a few weeks, allowing us to dive deeper into these performance-boosting techniques. Happy optimizing!
According to the uvicorn documentation, the framework installs "Cython-based" dependencies like uvloop and httptools "if possible".
Using uvicorn, should automatically install and utilize the uvloop event loop, same for httptools.
While it's safe to assume that using uvicorn would generally address points 1 and 2, let's explore the actual implementation to understand the specific conditions that determine whether these optimizations can be applied, and if there are indeed used by Ralph.