Skip to content

Cache adapters in adapt call - #122

Merged
grantroch merged 10 commits into
masterfrom
cache-adapters
Aug 7, 2026
Merged

grantroch merged 10 commits into
masterfrom
cache-adapters

Conversation

@grantroch

Copy link
Copy Markdown
Contributor

Closes #121

This adds a cache to return an adapter from a class to an interface. In my testing of a project this reduced the time substantially.

This also attempts to purge adapters from the cache when a new classes or adapters are registered.

Without cache:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   137657    1.502    0.000   10.618    0.000 C:\Source\leapfrog-1\components\win64\lib\site-packages\pure_interface\interface.py:571(_get_adapter)

With cache:
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      284    0.003    0.000    0.024    0.000 C:\Source\leapfrog-1\components\win64\lib\site-packages\pure_interface\interface.py:585(_get_adapter)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an adapter lookup cache to InterfaceType.adapt() to avoid repeatedly recomputing _get_adapter(cls, type(obj)), with cache invalidation hooks on adapter/type registration to keep results correct.

Changes:

  • Introduces _pi._adapter_cache and uses it in InterfaceType.adapt() to memoize _get_adapter() results per (interface, obj_type).
  • Adds clear_adapter_caches() and wires it into InterfaceType.register() and register_adapter() for cache invalidation.
  • Adds a new test suite covering caching behavior and several invalidation scenarios.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pure_interface/interface.py Adds cache storage, cache-clearing helper, and uses cached adapter resolution inside InterfaceType.adapt()
pure_interface/adaption.py Clears adapter caches on register_adapter() to avoid stale cached adapter lookups
tests/test_adapter_cache.py Adds regression tests for caching and cache invalidation behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pure_interface/interface.py Resolved
Comment thread tests/test_adapter_cache.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

pure_interface/interface.py:106

  • _adapter_cache is a plain dict keyed by type, so it will keep strong references to every adapted runtime type and can grow without bound in long-running processes (especially when many dynamic/generated classes are seen). Consider using weakref.WeakKeyDictionary (similar to self.adapters) so cached entries don’t prevent type objects from being garbage-collected; update tests that compare the cache to {} accordingly (e.g., len(cache) == 0).
        self.adapters = weakref.WeakKeyDictionary()  # type: ignore
        self.registered_types = weakref.WeakSet()  # type: ignore
        self.structural_subclasses: Set[type] = set()
        self.impl_wrapper_type: Optional[type] = None
        self._adapter_cache: Dict[type, Optional[Callable]] = {}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread pure_interface/interface.py Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread tests/test_adapter_cache.py Resolved
Comment thread pure_interface/interface.py Resolved
Comment thread pure_interface/interface.py Resolved

@russell-stewart-seequent russell-stewart-seequent left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You walk back up the MRO for cache clearing when new adapters are added. I think the equivalent is missing for registered types. The suggested changes here might fix this.

Comment thread pure_interface/interface.py Resolved
Comment thread pure_interface/interface.py Resolved
Comment thread pure_interface/adaption.py Resolved
Comment thread pure_interface/interface.py Resolved
@grantroch
grantroch merged commit b95028f into master Aug 7, 2026
3 checks passed
@grantroch
grantroch deleted the cache-adapters branch August 7, 2026 03:21
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.

InterfaceType.adapt should allow for caching adapter

5 participants