Skip to content

perf: cache decorator factories - #334

Draft
BobTheBuidler wants to merge 1 commit into
ethereum:mainfrom
BobTheBuidler:perf/cache-decorator-factories
Draft

perf: cache decorator factories#334
BobTheBuidler wants to merge 1 commit into
ethereum:mainfrom
BobTheBuidler:perf/cache-decorator-factories

Conversation

@BobTheBuidler

@BobTheBuidler BobTheBuidler commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Reuse repeated decorator factory results without changing decorator behavior.

What was wrong?

return_arg_type() and replace_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

Red panda

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread eth_utils/decorators.py
def _make_exception_map_cache_key(
old_to_new_exceptions: ExceptionMap,
) -> ExceptionMapCacheKey:
return (id(old_to_new_exceptions), tuple(old_to_new_exceptions.items()))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do we care?

Comment thread eth_utils/decorators.py Outdated

return wrapped

_replace_exceptions_cache[cache_key] = decorator

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@BobTheBuidler
BobTheBuidler marked this pull request as draft July 29, 2026 10:27
Reuse repeated decorator factory results while preserving existing wrapped-call and mutable exception mapping behavior.
@BobTheBuidler
BobTheBuidler force-pushed the perf/cache-decorator-factories branch from bc4fd7a to 4774937 Compare July 29, 2026 10:37
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.

1 participant