Skip to content

Explicit state and event id minification - #6100

Draft
benedikt-bartscher wants to merge 97 commits into
reflex-dev:mainfrom
benedikt-bartscher:explicit-event-id-minification
Draft

Explicit state and event id minification#6100
benedikt-bartscher wants to merge 97 commits into
reflex-dev:mainfrom
benedikt-bartscher:explicit-event-id-minification

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Add state and event name minification with CLI management tools.

  • Introduces minify.json config file to map state/event names to short IDs (e.g., myapp.state.AppState"a")
  • Adds reflex minify CLI command group for managing minification
  • Reduces frontend bundle size and WebSocket payload size by replacing long state/event paths with compact identifiers

CLI Commands

# Initialize minify.json with IDs for all states and events
reflex minify init

# Sync minify.json after code changes
reflex minify sync
reflex minify sync --prune --reassign-deleted

# Validate config against current codebase
reflex minify validate

# List state tree with minified names
reflex minify list
reflex minify list --json

# Lookup a minified path
reflex minify lookup "a.bU"

based on #6098

@codspeed-hq

codspeed-hq Bot commented Jan 24, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 14.19%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 4 improved benchmarks
✅ 23 untouched benchmarks
⏩ 8 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation test_from_event_type[event_spec] 49.2 µs 40.5 µs +21.46%
Simulation test_from_event_type[lambda_event_spec] 51.1 µs 42.7 µs +19.62%
Simulation test_from_event_type[event_handler] 91.9 µs 84.9 µs +8.31%
Simulation test_from_event_type[lambda_event_handler] 92.8 µs 85.9 µs +8.04%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing benedikt-bartscher:explicit-event-id-minification (d5107c1) with main (9f7f82c)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@benedikt-bartscher benedikt-bartscher changed the title Explicit event id minification Explicit state and event id minification Jan 25, 2026
Comment thread reflex/reflex.py Outdated
@benedikt-bartscher

benedikt-bartscher commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

TODO: best-effort-mode

  • walk trough the state id registry after the app module get's imported
  • filter out all states with explicit state id
  • sort the remaining states without state id by full name
  • assign state ids - start with the highest user defined state id - users might have fragmentation by choice

a similar thing can be implemented for event handler id's

edit: not that easy, as state names are baked into to many things (events, vars etc). we would need to defer init_subclass and possibly parts of @rx.var and @rx.event

edit2: i have found a better approach with a minify.json which makes this obsolete

@benedikt-bartscher

benedikt-bartscher commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

Open for discussion: Should reflex reserve the first 5/10 state id's for internal states? Current reflex uses 0-3, but that might change in the future.

edit: with sibling uniqueness we would just need to reserve the state id's on the first rx.State subclass level.

edit2: i have found a better approach with a minify.json which makes this obsolete

@benedikt-bartscher

benedikt-bartscher commented Jan 28, 2026

Copy link
Copy Markdown
Contributor Author

TODO: think about sibling uniqueness for states. currently using global uniqueness

edit: adjusted in 6b4c5fa

@benedikt-bartscher
benedikt-bartscher force-pushed the explicit-event-id-minification branch from 35ad1c6 to 1647c1e Compare February 1, 2026 10:25
@benedikt-bartscher

Copy link
Copy Markdown
Contributor Author

Idea: store ids in a minify.json file

  • allows overwriting state ids for upstream dependencies and reflex core
  • one centralized place for the ids
  • keep track of reserved numbers
  • better automatic instrumentation - state and event ids can all be managed with cli - without manual code changes
  • can be loaded once on startup and is frozen - probably faster
  • no best effort mode needed

When a parent and child state have the same minified name, substate
resolution can fail because the leading segment is stripped incorrectly. This
change adds a flag to skip stripping only on the initial recursive call, ensuring
correct resolution even in name collision scenarios.
@benedikt-bartscher
benedikt-bartscher marked this pull request as draft May 6, 2026 19:58
Comment thread reflex/reflex.py Outdated
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review July 9, 2026 22:46

@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: af4219fac8

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread reflex/reflex.py Outdated
Comment thread reflex/minify.py
Comment thread reflex/reflex.py Outdated
Comment thread reflex/reflex.py Outdated
Comment thread reflex/reflex.py Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 16 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/reflex-base/src/reflex_base/utils/format.py">

<violation number="1" location="packages/reflex-base/src/reflex_base/utils/format.py:489">
P1: When forked registration contexts use different name resolvers, this handler-level cache leaks the formatted event name between apps. Key the cache by registration context/resolver, or remove the instance cache so each context resolves its own event name.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment on lines +489 to +495
cached = handler.__dict__.get(_FORMATTED_NAME_CACHE_ATTR)
if cached is not None:
return cached
state, name = get_event_handler_parts(handler)
if state == "":
return name
return f"{state}.{name}"
full = name if state == "" else f"{state}.{name}"
object.__setattr__(handler, _FORMATTED_NAME_CACHE_ATTR, full)
return full

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: When forked registration contexts use different name resolvers, this handler-level cache leaks the formatted event name between apps. Key the cache by registration context/resolver, or remove the instance cache so each context resolves its own event name.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/reflex-base/src/reflex_base/utils/format.py, line 489:

<comment>When forked registration contexts use different name resolvers, this handler-level cache leaks the formatted event name between apps. Key the cache by registration context/resolver, or remove the instance cache so each context resolves its own event name.</comment>

<file context>
@@ -444,43 +444,55 @@ def format_props(*single_props, **key_value_props) -> list[str]:
     Returns:
         The formatted function.
     """
+    cached = handler.__dict__.get(_FORMATTED_NAME_CACHE_ATTR)
+    if cached is not None:
+        return cached
</file context>
Suggested change
cached = handler.__dict__.get(_FORMATTED_NAME_CACHE_ATTR)
if cached is not None:
return cached
state, name = get_event_handler_parts(handler)
if state == "":
return name
return f"{state}.{name}"
full = name if state == "" else f"{state}.{name}"
object.__setattr__(handler, _FORMATTED_NAME_CACHE_ATTR, full)
return full
state, name = get_event_handler_parts(handler)
return name if state == "" else f"{state}.{name}"

Comment thread packages/reflex-base/src/reflex_base/registry.py
Comment thread packages/reflex-base/src/reflex_base/compiler/templates.py
Comment thread reflex/minify.py
Comment thread reflex/minify.py
Comment thread packages/reflex-base/src/reflex_base/.templates/web/utils/state.js
Comment thread packages/reflex-base/src/reflex_base/.templates/web/utils/state.js
Comment thread reflex/minify.py
Comment thread tests/units/test_minification.py

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread reflex/reflex.py
@benedikt-bartscher
benedikt-bartscher marked this pull request as draft August 26, 2026 22:14
Comment thread reflex/reflex.py
Comment on lines +1382 to +1388
for part in parts:
# Find the child of the previous match whose minified id is ``part``.
found = next(
(
c
for c in current.get_substates()
if path_to_id.get(get_state_full_path(c)) == part

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Event lookup fails lookup still walks every remaining segment as a state id, so the documented copied frontend event form remains broken. For a valid minified event name like a.bU, the first segment can resolve to the user state, but the final bU is an event id from config["events"], not a child state id in config["states"]. Because this loop only checks current.get_substates() and path_to_id, it exits with No state found for minified segment 'bU' instead of resolving the event handler. The command needs to treat the final segment as a handler id for the resolved state, or otherwise reject and document state-only paths.

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.

7 participants