Skip to content

Explicit state and event id minification#6100

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

Explicit state and event id minification#6100
benedikt-bartscher wants to merge 89 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 13.48%

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

Performance Changes

Benchmark BASE HEAD Efficiency
test_from_event_type[event_spec] 48.8 µs 40.5 µs +20.58%
test_from_event_type[lambda_event_spec] 50.8 µs 42.7 µs +19.03%
test_from_event_type[event_handler] 91 µs 84.6 µs +7.58%
test_from_event_type[lambda_event_handler] 92.1 µs 85.7 µs +7.42%

Tip

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


Comparing benedikt-bartscher:explicit-event-id-minification (44fefa1) with main (f79d011)2

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.

  2. No successful run was found on main (e54bc83) during the generation of this report, so f79d011 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@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
has_substates = len(substates) > 0

if handlers:
console.log(f"{child_prefix}|-- Event Handlers:")

@abulvenz abulvenz Jan 27, 2026

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.

Very tiny optical nit, cause sometimes EventHandlers is the last sub item.
It might work like this, didn't test it.

Suggested change
console.log(f"{child_prefix}|-- Event Handlers:")
console.log(f"{child_prefix}{'|' if has_substates else '`'}-- Event Handlers:")

@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 ready for review April 26, 2026 19:05
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner April 26, 2026 19:05
@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
if output_json:
import json

console.log(json.dumps(tree_data, indent=2))

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 Emit raw JSON for minify list

When users run reflex minify list --json, this still goes through console.log, which uses Rich log output and honors the current log level, so stdout can be prefixed or suppressed instead of being parseable JSON. minify lookup --json uses click.echo; this branch should do the same so scripts can reliably consume it.

Useful? React with 👍 / 👎.

Comment thread reflex/minify.py
errors: list[str] = []
warnings: list[str] = []

all_states = collect_all_states(root_state)

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 Ignore framework states during validation

When minify.json intentionally contains only user-owned entries, this unfiltered state list makes reflex minify validate report missing framework states and handlers even though MinifyNameResolver never minifies framework modules and runtime falls back to their original names. Filter the same framework-owned states here before computing missing state/event entries so valid user-only configs do not fail validation.

Useful? React with 👍 / 👎.

Comment thread reflex/reflex.py Outdated
Comment thread reflex/reflex.py
Comment on lines +1261 to +1274
for i, part in enumerate(parts):
# Find the state whose minified id matches ``part``: the root state
# for the first segment, otherwise a child of the previous match.
candidates = [current] if i == 0 else current.get_substates()
found = next(
(c for c in candidates if path_to_id.get(get_state_full_path(c)) == part),
None,
)
if found is None:
console.error(
f"No state found for minified segment '{part}' in path '{minified_path}'"
)
raise SystemExit(1)

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 minify_lookup always fails for any input

For i == 0, candidates = [current] where current = State (the framework root). path_to_id.get(get_state_full_path(State)) is always None because State is in _FRAMEWORK_STATE_MODULES and is never given a minified ID. The comparison None == part is False for any non-empty input, so found is always None and every lookup immediately exits with an error.

For the PR's documented reflex minify lookup "a.bU" example, "a" is the ID of a direct child of State (a user root state). The first-segment candidates should be State.get_substates(), not [State] itself — matching the same pattern used for subsequent segments.

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