perf(dependencies): skip doing any work when sub dependant is already cached

This commit is contained in:
Gustav Bylund 2024-03-20 18:30:37 +01:00
parent 7e05bff3d4
commit 0264bcbe80
1 changed files with 12 additions and 5 deletions

View File

@ -590,6 +590,17 @@ async def solve_dependencies(
dependency_cache = {} dependency_cache = {}
for sub_dependant in dependant.dependencies: for sub_dependant in dependant.dependencies:
sub_dependant.call = cast(Callable[..., Any], sub_dependant.call) sub_dependant.call = cast(Callable[..., Any], sub_dependant.call)
if sub_dependant.use_cache:
# Use a unique object to compare against in case the cached value is None
cache_miss = object()
cached_value = dependency_cache.get(sub_dependant.cache_key, cache_miss)
# If the sub dependant is already cached, skip doing any more work
if cached_value is not cache_miss:
if sub_dependant.name is not None:
values[sub_dependant.name] = cached_value
continue
call = sub_dependant.call call = sub_dependant.call
use_sub_dependant = sub_dependant use_sub_dependant = sub_dependant
if ( if (
@ -624,11 +635,7 @@ async def solve_dependencies(
if solved_result.errors: if solved_result.errors:
errors.extend(solved_result.errors) errors.extend(solved_result.errors)
continue continue
if sub_dependant.use_cache and sub_dependant.cache_key in dependency_cache: if use_sub_dependant.is_gen_callable or use_sub_dependant.is_async_gen_callable:
solved = dependency_cache[sub_dependant.cache_key]
elif (
use_sub_dependant.is_gen_callable or use_sub_dependant.is_async_gen_callable
):
use_astack = request_astack use_astack = request_astack
if sub_dependant.scope == "function": if sub_dependant.scope == "function":
use_astack = function_astack use_astack = function_astack