perf(events): share one chain per handler and trigger across call sites

Merged
FA
2mo ago
+318-13

Summary

Second of three stacked compile-performance PRs. Stacked on #7121; this diff includes it. Merge #7121 first.

  • EventChain.create interns one chain per handler, args spec, and trigger key in RegistrationContext._bound_event_chains, keyed by (id(handler), id(args_spec), key) with the handler and spec held in the value so the ids stay valid. A handler bound to one trigger always produces the same chain, so every call site sharing the handler now shares one instance. Handlers carry no state, so deep-copying a component tree copies only the chains it contains, and a forked context starts with its own chains. Handlers with event_chain_kwargs, handlers carrying event actions (which are distinct dataclasses.replace copies), lists, lambdas, and EventSpec values are unaffected.
  • get_memoized_event_triggers keys its wrapper cache by trigger name and chain identity, holding the chain so its id cannot be recycled. The memo name stays content-hashed, so generated output is unchanged.

Why

The memoize pass rendered a fresh LiteralEventChainVar for every component to name its useCallback wrapper: ~240 µs per chain (LiteralVar.create + str + _get_all_var_data). With a hundred buttons bound to one handler that was the single largest item in the profile.

Measurements

Benchmark page with 100 rows bound to one handler (_repeated_stateful_page, full-context compile): 87.8 ms to 60.7 ms together with #7121, of which most is this change.

Docs site dry compile (511 routes, few handlers): 40.0 s (#7121) to 39.3 to 39.6 s. EventChain.create calls fell from 11.1k to 4.8k.

Micro-benchmarks, one handler bound to 8 triggers across 200 buttons, best of 5:

stateEventChain.create, repeated call sitecopy.deepcopy of the treebuild the tree
main4301 ns2.59 ms4.20 ms
cache on the handler (first revision)349 ns1.09 ms1.91 ms
cache on RegistrationContext (this PR)436 ns1.09 ms2.01 ms

The context-owned cache costs one context-variable lookup per hit. The handler-owned revision let copy.deepcopy walk into the handler and duplicate every chain bound to it, which scaled with the number of triggers per handler; the context-owned map removes that walk.

Design note for review

Call sites that bind the same handler to the same trigger now share one EventChain object. Chains are frozen dataclasses and nothing in the framework mutates events or event_actions in place (all action helpers return replaced copies), but this is a visible change in object identity.

Test plan

  • New tests: chain interning by handler, args spec, key, kwargs, and event actions; wrapper reuse by chain identity without rendering; distinct wrappers for chains that differ in captured args or actions.
  • test_event_chain_cache_lives_on_the_registration_context: a forked context yields its own chain, the parent's chain comes back after the fork exits, and the handler retains no reference to any chain.
  • tests/units/test_event.py, tests/units/reflex_base, tests/units/components, memoize plugin tests green apart from failures that reproduce on clean main here.
  • ruff, pyright, pre-commit clean.

https://claude.ai/code/session_01PmizE1eQhtYZyVs1RK2ke3