Reduce memory usage by Dependant by ~50%#15336
Open
ipeluffo wants to merge 16 commits into
Open
Conversation
``` 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 ```
``` 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 ```
… 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 ```
``` 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 ```
…ory 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 ```
``` 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 ```
…ry 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 ```
…ry 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 ```
``` 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 ```
``` Samples: 200000 - current=156.8 MB peak=156.8 MB Samples: 100000 - current=78.4 MB peak=78.4 MB Samples: 50000 - current=39.2 MB peak=39.2 MB Samples: 1000 - current=0.8 MB peak=0.8 MB Samples: 500 - current=0.4 MB peak=0.4 MB ```
ipeluffo
commented
Apr 14, 2026
Comment on lines
+21
to
+25
| unwrapped = inspect.unwrap(cast(Callable[..., Any], _impartial(call))) | ||
| return unwrapped | ||
|
|
||
|
|
||
| def _impartial(func: Callable[..., Any]) -> Callable[..., Any]: | ||
| def _impartial(func: Callable[..., Any] | None) -> Callable[..., Any] | None: |
Author
There was a problem hiding this comment.
[info] This is unrelated to my PR but mypy was complaining in the pre-commit check. The change looks safe and sensible
|
@YuriiMotov is there anything needed to move this one forward and get some feedback/review? Thanks |
Author
|
@tiangolo , I'd be great if we can some feedback on this before the PR gets lost. Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
In #14742, I reported a significant spike in memory usage when upgrading FastAPI from v0.120.4 to v0.121.x (and later versions).
My analysis traced the regression to this refactor, which replaced computed fields in
__post_init__withfunctools.cached_property.Root cause
The Python docs for
functools.cached_propertynote:While the worst of this was addressed in Python 3.12+ python/cpython#101815, there is still an overhead: accessing
__dict__on an instance forces creation of a full dictionary object rather than a compactPyDictValues, adding aPyDictObject(three fields) per instance. At scale, this adds up.The exact quote from the cpython issue:
Benchmarks
I wrote a script to measure memory usage across varying numbers of
Dependantinstances:Benchmark script
Before (current
master, Python 3.13.12):After (this PR):
Memory usage is reduced by approximately 50% across all cases.
Approach
The key enabler for the memory reduction is the use of
__slots__. However,__slots__is not compatible withfunctools.cached_property, so the caching strategy had to change.I considered reverting to the previous
__post_init__approach but decided against it for two reasons:Dependantinstantiation, even if never accessed.__post_init__harder to maintain.Instead, this PR introduces a different caching mechanism that preserves lazy evaluation while being compatible with
__slots__.