-
Notifications
You must be signed in to change notification settings - Fork 1.8k
perf(events): share one chain per handler and trigger across call sites #7122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
masenf
merged 10 commits into
reflex-dev:main
from
FarhanAliRaza:farhan/event-chain-interning
Sep 17, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9300edf
perf(compile): read only set props and cache literal Var dispatch
FarhanAliRaza 4715789
test(vars): restore registries after the literal dispatch test and ad…
FarhanAliRaza 53b069f
perf(events): share one chain per handler and trigger across call sites
FarhanAliRaza 10ebca4
perf(events): keep bound chain cache on the RegistrationContext
FarhanAliRaza db57926
perf(events): skip the chain cache for handlers carrying event actions
FarhanAliRaza bf0d021
test(events): run the chain cache test on a forked context
FarhanAliRaza 83f4f45
Merge remote-tracking branch 'upstream/main' into farhan/event-chain-…
FarhanAliRaza 45943ef
fix(compiler): drop the event chain and wrapper caches at the start o…
FarhanAliRaza f0255e8
test(events): state why the cached chain is re-asserted after the eve…
FarhanAliRaza e014e06
Merge branch 'main' into farhan/event-chain-interning
masenf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Share one event chain per handler and trigger across call sites, and reuse memoized event wrappers by chain identity during compilation. |
1 change: 1 addition & 0 deletions
1
packages/reflex-base/news/+event-chain-interning.performance.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Share one event chain per handler and trigger across call sites, and reuse memoized event wrappers by chain identity during compilation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
tests/units/reflex_base/components/test_memoize_helpers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| """Tests for sharing prepared event wrappers within a registration context.""" | ||
|
|
||
| import dataclasses | ||
|
|
||
| import pytest | ||
| from reflex_base.components.component import Component | ||
| from reflex_base.components.memoize_helpers import get_memoized_event_triggers | ||
| from reflex_base.event import EventChain, EventHandler, no_args_event_spec | ||
| from reflex_base.registry import RegistrationContext | ||
| from reflex_base.utils.imports import ImportVar | ||
| from reflex_base.vars.base import LiteralVar, Var, VarData | ||
|
|
||
|
|
||
| def test_event_wrappers_are_reused_and_reset_with_context(): | ||
| """Identical wrappers share work only within their owning context.""" | ||
| component = Component._create( | ||
| children=(), event_triggers={"on_click": Var("handler", EventChain)} | ||
|
masenf marked this conversation as resolved.
|
||
| ) | ||
| with RegistrationContext.ensure_context().fork() as context: | ||
| first = get_memoized_event_triggers(component)["on_click"] | ||
| assert get_memoized_event_triggers(component)["on_click"] is first | ||
| with context.fork() as fork: | ||
| assert not fork._memoized_event_triggers | ||
| assert get_memoized_event_triggers(component)["on_click"] is not first | ||
| context._memoized_event_triggers.clear() | ||
| assert get_memoized_event_triggers(component)["on_click"] is not first | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("first_data", "second_data"), | ||
| [ | ||
| (VarData(state="first"), VarData(state="second")), | ||
| ( | ||
| VarData(hooks=["const first = useFirst()"]), | ||
| VarData(hooks=["const second = useSecond()"]), | ||
| ), | ||
| ( | ||
| VarData(imports={"first": [ImportVar("value")]}), | ||
| VarData(imports={"second": [ImportVar("value")]}), | ||
| ), | ||
| (VarData(deps=[Var("first")]), VarData(deps=[Var("second")])), | ||
| ], | ||
| ) | ||
| def test_event_wrapper_cache_preserves_dependencies( | ||
| first_data: VarData, second_data: VarData | ||
| ): | ||
| """Identical expressions with different metadata must keep their dependencies.""" | ||
| with RegistrationContext.ensure_context().fork(): | ||
| first = get_memoized_event_triggers( | ||
| Component._create( | ||
| children=(), | ||
| event_triggers={"on_click": Var("handler", EventChain, first_data)}, | ||
| ) | ||
| )["on_click"] | ||
| second = get_memoized_event_triggers( | ||
| Component._create( | ||
| children=(), | ||
| event_triggers={"on_click": Var("handler", EventChain, second_data)}, | ||
| ) | ||
| )["on_click"] | ||
| assert first is not second | ||
| assert repr(first._get_all_var_data()) != repr(second._get_all_var_data()) | ||
|
|
||
|
|
||
| def test_event_wrapper_cache_preserves_provider_identity(): | ||
| """Providers sharing a role can still carry distinct component props.""" | ||
| first_provider = Component._create( | ||
| children=(), tag="Provider", custom_attrs={"value": "first"} | ||
| ) | ||
| second_provider = Component._create( | ||
| children=(), tag="Provider", custom_attrs={"value": "second"} | ||
| ) | ||
| with RegistrationContext.ensure_context().fork(): | ||
| for provider in (first_provider, second_provider): | ||
| event = Var("handler", EventChain, VarData(app_wraps=[(10, provider)])) | ||
| wrapper = get_memoized_event_triggers( | ||
| Component._create(children=(), event_triggers={"on_click": event}) | ||
| )["on_click"] | ||
| data = wrapper._get_all_var_data() | ||
| assert data is not None | ||
| assert data.app_wraps[0][1] is provider | ||
|
|
||
|
|
||
| def test_event_wrapper_cache_does_not_compare_vars_as_python_booleans(): | ||
| """Equivalent dependency expressions may belong to different Var objects.""" | ||
| with RegistrationContext.ensure_context().fork(): | ||
| for _ in range(2): | ||
| event = Var("handler", EventChain, VarData(deps=[Var("dependency")])) | ||
| wrapper = get_memoized_event_triggers( | ||
| Component._create(children=(), event_triggers={"on_click": event}) | ||
| )["on_click"] | ||
| data = wrapper._get_all_var_data() | ||
| assert data is not None | ||
| assert {str(dep) for dep in data.deps} == {"dependency"} | ||
|
|
||
|
|
||
| def test_event_wrapper_reflects_captured_arguments_and_actions(): | ||
| """Chains differing in nested data compile to different wrappers.""" | ||
|
|
||
| def handler(value: str): | ||
| """Accept an event argument.""" | ||
|
|
||
| def chain(argument: str, **actions: bool) -> EventChain: | ||
| """Build a chain for one handler call. | ||
|
|
||
| Args: | ||
| argument: The captured handler argument. | ||
| **actions: Event actions applied to the nested event. | ||
|
|
||
| Returns: | ||
| The chain wrapping the handler call. | ||
| """ | ||
| spec = EventHandler(fn=handler)(argument) | ||
| if actions: | ||
| spec = dataclasses.replace(spec, event_actions=actions) | ||
| return EventChain(events=[spec], args_spec=no_args_event_spec) | ||
|
|
||
| component = Component._create(children=(), event_triggers={}) | ||
| with RegistrationContext.ensure_context().fork(): | ||
| rendered = [] | ||
| for event in ( | ||
| chain("first"), | ||
| dataclasses.replace(chain("first"), event_actions={"preventDefault": True}), | ||
| chain("first", stopPropagation=True), | ||
| chain("second"), | ||
| ): | ||
| component.event_triggers["on_click"] = event | ||
| rendered.append(str(get_memoized_event_triggers(component)["on_click"])) | ||
| assert len(set(rendered)) == len(rendered) | ||
|
|
||
|
|
||
| def test_event_wrappers_are_shared_by_chain_identity(monkeypatch): | ||
| """Components bound to one chain object share one wrapper without rendering it.""" | ||
| chain = Var("handler", EventChain) | ||
| first = Component._create(children=(), event_triggers={"on_click": chain}) | ||
| second = Component._create(children=(), event_triggers={"on_click": chain}) | ||
| other_trigger = Component._create(children=(), event_triggers={"on_blur": chain}) | ||
| with RegistrationContext.ensure_context().fork(): | ||
| wrapper = get_memoized_event_triggers(first)["on_click"] | ||
| monkeypatch.setattr(LiteralVar, "create", pytest.fail) | ||
| assert get_memoized_event_triggers(second)["on_click"] is wrapper | ||
| monkeypatch.undo() | ||
| assert get_memoized_event_triggers(other_trigger)["on_blur"] is not wrapper | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.