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

📝 Update docs for ORJSONResponse with details about improving performance #2615

Merged
merged 4 commits into from Sep 1, 2022

Conversation

falkben
Copy link
Sponsor Contributor

@falkben falkben commented Jan 7, 2021

Update docs to mention to explicitly return a Response when thinking about performance. Currently the docs say to use orjson for speed, but then demonstrate returning a dictionary, which in my experience can be very slow. The main speed improvement I have found is in returning Responses directly, which avoids the call to jsonable_encoder.

Below are results from some tests I did with 7MB+ json data. These are real world and include round trips to the database so to see such a huge effect is rather amazing to me.

To be clear, the difference here is just returning an explicit ORJSONResponse object, vs. JSONResponse object vs. a dict and letting fastapi inspect it. The data returned are exactly the same in all cases.

For more examples w/ code demonstrating the differences see here:
https://github.com/falkben/fastapi_experiments/blob/master/experiments/orjson_response.py

and some tests here:
https://github.com/falkben/fastapi_experiments/blob/master/experiments/test_orjson_response.py

In those tests I observed a 6x slowdown when returning a dictionary vs. returning a Response directly

This was also discussed in the following issue: #360

At some point it would be good to try to optimize jsonable_encoder but for now I think it would be good to at least tell people about the performance impact of returning large dictionaries.

@codecov
Copy link

codecov bot commented Jan 7, 2021

Codecov Report

Merging #2615 (2106ac4) into master (278adc0) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master     #2615   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          537       537           
  Lines        13856     13856           
=========================================
  Hits         13856     13856           
Impacted Files Coverage Δ
docs_src/custom_response/tutorial001b.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2021

📝 Docs preview for commit 8b3b6c4 at: https://5ff7571877e9d83cd685c1ef--fastapi.netlify.app

@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2021

📝 Docs preview for commit 2071f19 at: https://601eb84f0aa75d20b894c8cf--fastapi.netlify.app

urubens added a commit to Cytomine-ULiege/pims that referenced this pull request Dec 22, 2021
urubens added a commit to Cytomine-ULiege/pims that referenced this pull request Jan 6, 2022
@tiangolo tiangolo changed the title update performance related orjson docs 📝 Update docs for ORJSONResponse with details about improving performance Sep 1, 2022
@tiangolo
Copy link
Owner

tiangolo commented Sep 1, 2022

Awesome, thank you @falkben! ☕

I updated the text a little bit to explain the jsonable_encoder part a bit more. Thanks for the contribution! 🚀

@tiangolo tiangolo enabled auto-merge (squash) September 1, 2022 09:57
@tiangolo tiangolo merged commit a71077c into tiangolo:master Sep 1, 2022
@falkben falkben deleted the falkben/update_orjson_docs branch September 1, 2022 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants