Skip to content

Commit 613d133

Browse files
committed
Add caching for function signatures to improve performance
1 parent 6e4794b commit 613d133

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/dependency_injector/wiring.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,18 @@ def _extract_marker(parameter: inspect.Parameter) -> Optional["_Marker"]:
591591
return marker
592592

593593

594-
def _fetch_reference_injections( # noqa: C901
594+
_signature_cache: Dict[int, Tuple[Dict[str, Any], Dict[str, Any]]] = {}
595+
596+
597+
def _fetch_reference_injections(
595598
fn: Callable[..., Any],
596599
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
600+
"""Get reference injections with caching."""
601+
# Use the function's id as cache key
602+
fn_id = id(fn)
603+
if fn_id in _signature_cache:
604+
return _signature_cache[fn_id]
605+
597606
# Hotfix, see:
598607
# - https://github.com/ets-labs/python-dependency-injector/issues/362
599608
# - https://github.com/ets-labs/python-dependency-injector/issues/398
@@ -606,9 +615,13 @@ def _fetch_reference_injections( # noqa: C901
606615
signature = inspect.signature(fn)
607616
except ValueError as exception:
608617
if "no signature found" in str(exception):
609-
return {}, {}
618+
result = {}, {}
619+
_signature_cache[fn_id] = result
620+
return result
610621
elif "not supported by signature" in str(exception):
611-
return {}, {}
622+
result = {}, {}
623+
_signature_cache[fn_id] = result
624+
return result
612625
else:
613626
raise exception
614627

@@ -625,7 +638,11 @@ def _fetch_reference_injections( # noqa: C901
625638
closing[parameter_name] = marker
626639

627640
injections[parameter_name] = marker
628-
return injections, closing
641+
642+
# Cache the result
643+
result = (injections, closing)
644+
_signature_cache[fn_id] = result
645+
return result
629646

630647

631648
def _locate_dependent_closing_args(
@@ -1036,4 +1053,5 @@ def _patched(*args, **kwargs):
10361053
patched.injections,
10371054
patched.closing,
10381055
)
1056+
10391057
return cast(F, _patched)

0 commit comments

Comments
 (0)