Skip to content

fix: more cached type hints#6161

Open
benedikt-bartscher wants to merge 3 commits intoreflex-dev:mainfrom
benedikt-bartscher:more-cached-type-hints
Open

fix: more cached type hints#6161
benedikt-bartscher wants to merge 3 commits intoreflex-dev:mainfrom
benedikt-bartscher:more-cached-type-hints

Conversation

@benedikt-bartscher
Copy link
Contributor

No description provided.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 6, 2026

Merging this PR will not alter performance

✅ 8 untouched benchmarks


Comparing benedikt-bartscher:more-cached-type-hints (fb3f51b) with main (3c11451)

Open in CodSpeed

@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review March 6, 2026 21:55
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 6, 2026

Greptile Summary

This PR is a small, focused performance improvement that extends the "cached type hints" pattern to the event-handler processing path in BaseState. It replaces the uncached typing.get_type_hints(handler.fn) call with types.get_type_hints(handler.fn) — the @lru_cache-wrapped wrapper in reflex.utils.types — so that repeated lookups of the same function's type annotations hit the cache rather than re-evaluating annotations on every event dispatch. The standalone import typing is removed as it is no longer referenced.

Changes:

  • typing.get_type_hints(handler.fn)types.get_type_hints(handler.fn) (cached), avoiding redundant annotation evaluation on each event dispatch.
  • import typing removed — no remaining typing.X usages exist in the file.
  • The directly-imported get_type_hints (line 32, from typing import get_type_hints) is retained because it is still used at line 814 (with a localns= argument that the cached wrapper does not support) and at line 2535.

Why safe: The change is minimal and well-scoped: it replaces one uncached typing.get_type_hints call with the project's own @lru_cache-decorated wrapper. The cache key (handler.fn) is a stable, hashable function object. No behavioural change is introduced — only a caching improvement on every event dispatch.

Confidence Score: 5/5

  • This PR is safe to merge — it is a two-line focused change with no logic alteration, only a switch to the already-established cached wrapper.
  • The change is minimal and well-scoped: it replaces one uncached typing.get_type_hints call with the project's own @lru_cache-decorated wrapper, and correctly removes the now-unused import typing. The cache key (handler.fn) is a stable, hashable function object. No behavioural change is introduced — only a caching improvement on every event dispatch.
  • No files require special attention.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Event dispatched to BaseState._process_event] --> B[Build functools.partial fn from handler.fn]
    B --> C{types.get_type_hints\nhandler.fn}
    C -- Cache hit --> D[Return cached type hints dict]
    C -- Cache miss --> E[typing.get_type_hints_og\nevaluates annotations]
    E --> F[Store result in lru_cache]
    F --> D
    D --> G[Iterate payload args]
    G --> H{Type hint found\nfor arg?}
    H -- No / Any --> I[Skip coercion]
    H -- Yes --> J[Coerce value to hinted type]
    I --> K[Process next arg]
    J --> K
Loading

Last reviewed commit: 39da464

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