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

♻ Change a dict() for {} in fastapi/utils.py #3138

Merged
merged 1 commit into from Aug 26, 2022

Conversation

ShahriyarR
Copy link
Contributor

In general dict() call is considered slower than direct {}.
The internal representations are also different, there is no extra function call while constructing the map with {}

>>> import dis
>>> dis.dis('{}')
  1           0 BUILD_MAP                0
              2 RETURN_VALUE
>>> dis.dis('dict()')
  1           0 LOAD_NAME                0 (dict)
              2 CALL_FUNCTION            0
              4 RETURN_VALUE

There are bunch of similar cases in FastAPI source code, I will send PRs accordingly.

@codecov
Copy link

codecov bot commented May 1, 2021

Codecov Report

Merging #3138 (3a91387) into master (ce0ec06) will not change coverage.
The diff coverage is n/a.

@@             Coverage Diff             @@
##            master     #3138     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files          243       531    +288     
  Lines         7419     13629   +6210     
===========================================
+ Hits          7419     13629   +6210     
Impacted Files Coverage Δ
fastapi/utils.py 100.00% <ø> (ø)
fastapi/params.py 100.00% <0.00%> (ø)
fastapi/routing.py 100.00% <0.00%> (ø)
fastapi/encoders.py 100.00% <0.00%> (ø)
fastapi/responses.py 100.00% <0.00%> (ø)
fastapi/exceptions.py 100.00% <0.00%> (ø)
fastapi/concurrency.py 100.00% <0.00%> (ø)
fastapi/applications.py 100.00% <0.00%> (ø)
fastapi/openapi/docs.py 100.00% <0.00%> (ø)
tests/test_ws_router.py 100.00% <0.00%> (ø)
... and 318 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2252837...3a91387. Read the comment docs.

@github-actions
Copy link
Contributor

github-actions bot commented May 1, 2021

📝 Docs preview for commit 8f737f7 at: https://608d2773115ff8084ddb1b76--fastapi.netlify.app

Copy link
Contributor

@cikay cikay left a comment

Choose a reason for hiding this comment

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

The commit message should be "Replace empty dict literal with built-in dict function"

You can also specifiy the reason in the body of the commit which should be performance optimization

Description: Performance optimization. Replacing the dict() calls with direct {} statements
@github-actions
Copy link
Contributor

📝 Docs preview for commit 3a91387 at: https://627e32546e589151aede165c--fastapi.netlify.app

@tiangolo tiangolo changed the title Replaced dict() call with direct {} usage in fastapi/utils.py ♻ Change a dict() for {} in fastapi/utils.py Aug 26, 2022
@tiangolo
Copy link
Owner

Thanks @ShahriyarR! 🚀

Also, don't worry about the commit messages, I update the PR title and squash and merge afterwards, so I take care of keeping the commits consistent with the style of the code.

@tiangolo tiangolo merged commit 00bdf53 into tiangolo:master Aug 26, 2022
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

6 participants