Browse Source

Refactor `Dependant.computed_scope` property for improved memory usage

```
Samples: 200000 - current=318.4 MB  peak=318.4 MB
Samples: 100000 - current=159.2 MB  peak=159.2 MB
Samples: 50000 - current=79.6 MB  peak=79.6 MB
Samples: 1000 - current=1.6 MB  peak=1.6 MB
Samples: 500 - current=0.8 MB  peak=0.8 MB
```
pull/15336/head
ipeluffo 3 months ago
parent
commit
da148fb8a3
Failed to extract signature
  1. 19
      fastapi/dependencies/models.py

19
fastapi/dependencies/models.py

@ -59,6 +59,7 @@ class Dependant:
_is_gen_callable_cache: bool = field(default=None, init=False, repr=False) _is_gen_callable_cache: bool = field(default=None, init=False, repr=False)
_is_async_gen_callable_cache: bool = field(default=None, init=False, repr=False) _is_async_gen_callable_cache: bool = field(default=None, init=False, repr=False)
_is_coroutine_callable_cache: bool = field(default=None, init=False, repr=False) _is_coroutine_callable_cache: bool = field(default=None, init=False, repr=False)
_computed_scope_cache: str | None = field(default=None, init=False, repr=False)
@property @property
def oauth_scopes(self) -> list[str]: def oauth_scopes(self) -> list[str]:
@ -250,13 +251,17 @@ class Dependant:
self._is_coroutine_callable_cache = True self._is_coroutine_callable_cache = True
else: else:
self._is_coroutine_callable_cache = False self._is_coroutine_callable_cache = False
return self._is_coroutine_callable_cache return self._is_coroutine_callable_cache
@cached_property @property
def computed_scope(self) -> str | None: def computed_scope(self) -> str | None:
if self.scope: if self._computed_scope_cache is None:
return self.scope if self.scope:
if self.is_gen_callable or self.is_async_gen_callable: self._computed_scope_cache = self.scope
return "request" elif self.is_gen_callable or self.is_async_gen_callable:
return None self._computed_scope_cache = "request"
else:
self._computed_scope_cache = None
return self._computed_scope_cache

Loading…
Cancel
Save