Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fastapi perfs #391

Merged
merged 3 commits into from
Dec 20, 2024

Conversation

rcaillon-Iliad
Copy link
Contributor

For large responses, returning a Response directly is much faster than returning a dictionary.

This is because by default, FastAPI will inspect every item inside and make sure it is serializable as JSON, using the same JSON Compatible Encoder explained in the tutorial. This is what allows you to return arbitrary objects, for example database models.

But if you are certain that the content that you are returning is serializable with JSON, you can pass it directly to the response class and avoid the extra overhead that FastAPI would have by passing your return content through the jsonable_encoder before passing it to the response class.

If your application (somehow) doesn't have to communicate with anything else and wait for it to respond, use async def.

  • call orjson.dumps one time only

Copy link
Owner

@antonputra antonputra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@antonputra antonputra merged commit 7f945cd into antonputra:main Dec 20, 2024
NewSouthMjos added a commit to NewSouthMjos/tutorials that referenced this pull request Dec 20, 2024
NewSouthMjos added a commit to NewSouthMjos/tutorials that referenced this pull request Dec 20, 2024
antonputra pushed a commit that referenced this pull request Dec 20, 2024
* feat: litestar application

* Revert "Improve fastapi perfs (#391)"

This reverts commit 7f945cd.

* feat: moved litestar app to lesson 232

* fix: only direct dependencies are listed

* fix: dockerfile

* fix: logging, post request body

* fix: deleted litestar app from lesson 231

* fix: revert requirements.txt lesson 231

* Reapply "Improve fastapi perfs (#391)"

This reverts commit 19745b8.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants