Skip to content

Conversation

@nulinspiratie
Copy link
Contributor

@nulinspiratie nulinspiratie commented Oct 14, 2025

https://quantum-machines.atlassian.net/browse/QUAL-1338

This brought the library loading time down from 3.6s to ~1s

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]
Copy link
Collaborator

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))
Copy link
Collaborator

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>...)

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]
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants