improve library loading times by caching - #127
Open
nulinspiratie wants to merge 1 commit into
Open
Conversation
maxim-v4s
reviewed
Oct 22, 2025
Comment on lines
+78
to
+96
| def _cached_getsourcelines(obj: Any) -> tuple: | ||
| """Cached version of inspect.getsourcelines(). | ||
|
|
||
| Args: | ||
| obj: The object to get the source lines for | ||
|
|
||
| Returns: | ||
| Tuple of (lines, line_number) | ||
| """ | ||
| cache = _inspection_cache.get() | ||
| if cache is None: | ||
| # Not in a caching context, use original function | ||
| return _original_getsourcelines(obj) | ||
|
|
||
| # Use special key format to avoid collision with other caches | ||
| cache_key = ('sourcelines', id(obj)) | ||
| if cache_key not in cache: | ||
| cache[cache_key] = _original_getsourcelines(obj) | ||
| return cache[cache_key] |
Collaborator
There was a problem hiding this comment.
This function has never been called.
I guess it can be removed.
| return _original_getsourcefile(obj) | ||
|
|
||
| # Use special key format to avoid collision with getmodule cache | ||
| cache_key = ('sourcefile', id(obj)) |
Collaborator
There was a problem hiding this comment.
In _cached_getmodule cache key is (id(obj), _filename).
In _cached_getsourcefile cache key is ('sourcefile', id(obj)).
I guess key should has same key strucure ('<function_key>', <funciton args>...)
Comment on lines
+45
to
+54
| cache = _inspection_cache.get() | ||
| if cache is None: | ||
| # Not in a caching context, use original function | ||
| return _original_getmodule(obj, _filename) | ||
|
|
||
| # Use object id and filename as cache key | ||
| cache_key = (id(obj), _filename) | ||
| if cache_key not in cache: | ||
| cache[cache_key] = _original_getmodule(obj, _filename) | ||
| return cache[cache_key] |
Collaborator
There was a problem hiding this comment.
I'm confused a little.
A large percentage of calls contain a Frame object as an argument.
And it's a little strange to use id of Frame as cache key. But it's working here.
I'm still researching
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.
https://quantum-machines.atlassian.net/browse/QUAL-1338
This brought the library loading time down from 3.6s to ~1s