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
perf(dependencies): skip doing any work when sub dependant is already cached #11323
base: master
Are you sure you want to change the base?
Conversation
8bb7d6d
to
69fca88
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks really reasonable to check whether the sub_dependency is already solved and cached before solving it!
Related PR, that uses almost the same approach: #4089
fastapi/dependencies/utils.py
Outdated
if ( | ||
sub_dependant.use_cache | ||
and sub_dependant.cache_key in dependency_cache | ||
and sub_dependant.name is not None | ||
): | ||
values[sub_dependant.name] = dependency_cache[sub_dependant.cache_key] | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( | |
sub_dependant.use_cache | |
and sub_dependant.cache_key in dependency_cache | |
and sub_dependant.name is not None | |
): | |
values[sub_dependant.name] = dependency_cache[sub_dependant.cache_key] | |
continue | |
if ( | |
sub_dependant.use_cache | |
and sub_dependant.cache_key in dependency_cache | |
): | |
if sub_dependant.name is not None: | |
values[sub_dependant.name] = dependency_cache[sub_dependant.cache_key] | |
continue |
To skip solving dependencies without name, that were already cached.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense 👍
In which cases will a sub_dependant not have a name?
That PR also has a test! :) I think that other PR, with your suggested changes, is a better change than this PR. Do you think it makes more sense to close this in favour of the other one? |
4f9395e
to
c01036d
Compare
This solves a bug where an empty dictionary would be reassigned.
I've rebased and updated the PR with the suggested changes from @YuriiMotov and with the change to dependency_cache empty dict checking from #4089. |
Hi, we noticed very large performance gains after making this change for our app that heavily uses the dependency injection system. One of our slower endpoints went from taking ~1100ms to ~400ms.