perf: cache decorator factories - #334
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 381f119c8f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def _make_exception_map_cache_key( | ||
| old_to_new_exceptions: ExceptionMap, | ||
| ) -> ExceptionMapCacheKey: | ||
| return (id(old_to_new_exceptions), tuple(old_to_new_exceptions.items())) |
There was a problem hiding this comment.
Avoid hashing replacement exception classes
When the replacement is a valid exception class whose metaclass defines __hash__ = None, hashing this cache key raises TypeError before the decorator can be created. The previous implementation supported such classes because replacement classes only needed to be callable; build the key without requiring replacement values to be hashable.
Useful? React with 👍 / 👎.
|
|
||
| return wrapped | ||
|
|
||
| _replace_exceptions_cache[cache_key] = decorator |
There was a problem hiding this comment.
Bound the exception-factory cache
When applications or plugin systems construct mappings dynamically, every call with a new mapping stores a decorator that closes over that mapping, preventing both from being collected for the rest of the process. Mutating and reusing a mapping also leaves an entry for every historical item tuple, so long-running processes can accumulate memory without limit; use a bounded or weak-reference-aware strategy instead.
Useful? React with 👍 / 👎.
Reuse repeated decorator factory results while preserving existing wrapped-call and mutable exception mapping behavior.
bc4fd7a to
4774937
Compare
Reuse repeated decorator factory results without changing decorator behavior.
What was wrong?
return_arg_type()andreplace_exceptions()rebuilt equivalent decorator factories on repeated calls.Related to Issue #
Closes #
How was it fixed?
Added small caches for repeated factory calls. The exception factory cache keys on mapping identity plus current items, preserving existing mutable mapping behavior. Tests cover reuse and mutation cases.
Todo:
Cute Animal Picture