-
Couldn't load subscription status.
- Fork 2
improve library loading times by caching #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| 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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>...)
| 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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
https://quantum-machines.atlassian.net/browse/QUAL-1338
This brought the library loading time down from 3.6s to ~1s