Skip to content

Conversation

@benedikt-bartscher
Copy link
Contributor

new alternative to #3728

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 24, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing benedikt-bartscher:explicit-state-id-minification (0858c9e) with main (be43052)

Summary

✅ 8 untouched benchmarks

@benedikt-bartscher benedikt-bartscher changed the title feat: Add explicit state ID minification for states feat: Add explicit state name minification Jan 24, 2026
@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review January 24, 2026 20:03
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 24, 2026

Greptile Overview

Greptile Summary

This PR introduces explicit state name minification via state_id parameter, providing three modes: DISABLED (default, uses full names), ENABLED (minifies states with explicit state_id), and ENFORCE (requires all non-mixin states to have state_id).

Key Changes:

  • Added StateMinifyMode enum and REFLEX_MINIFY_STATES environment variable in reflex/environment.py
  • Implemented _int_to_minified_name() function using base-54 encoding (a-z, A-Z, $, _) to convert integer state IDs to compact names
  • Modified BaseState.__init_subclass__() to accept and validate state_id parameter, maintaining a global registry _state_id_registry to prevent duplicates
  • Updated BaseState.get_name() to return minified names based on the configured mode
  • Extended BaseStateMeta.__new__() to pass state_id parameter through to __init_subclass__
  • Assigned state_id=0 to the root State class
  • Added comprehensive unit and integration tests

Issues Found:

  • The _int_to_minified_name() function lacks validation for negative state_id values, which would return an empty string instead of raising a clear error

Confidence Score: 4/5

  • This PR is safe to merge with one minor fix needed for robustness
  • The implementation is well-designed with comprehensive tests covering all modes and edge cases. The code follows good practices with duplicate detection, proper error messages, and support for module reloading. The only issue is missing validation for negative state_id values in _int_to_minified_name(), which is a minor robustness concern that should be addressed before merging
  • reflex/state.py - add negative value validation in _int_to_minified_name()

Important Files Changed

Filename Overview
reflex/environment.py Added StateMinifyMode enum and REFLEX_MINIFY_STATES environment variable - clean, straightforward addition
reflex/vars/base.py Added state_id parameter to BaseStateMeta.new and correctly passes it to init_subclass
tests/units/test_state_minification.py Comprehensive unit tests covering all modes, edge cases, and validation logic
tests/integration/test_state_minification.py Integration tests verify state minification works end-to-end in both disabled and enabled modes
reflex/state.py Core implementation with state_id registry, minification logic, and validation - missing negative state_id validation in _int_to_minified_name

Sequence Diagram

sequenceDiagram
    participant App as User App Code
    participant Meta as BaseStateMeta.__new__
    participant Init as BaseState.__init_subclass__
    participant Reg as _state_id_registry
    participant GetName as BaseState.get_name()
    participant Minify as _int_to_minified_name()

    App->>Meta: Define class MyState(rx.State, state_id=42)
    Meta->>Meta: Parse state_id parameter
    Meta->>Meta: Set namespace["_mixin"] = False
    Meta->>Init: Call __init_subclass__(state_id=42)
    Init->>Init: Check if cls._mixin is True
    Init->>Init: Set cls._state_id = 42
    Init->>Reg: Check if state_id=42 exists
    alt state_id already registered
        Reg->>Init: Return existing class
        Init->>Init: Validate not duplicate (same class OK)
    else state_id not registered
        Reg->>Init: Not found
    end
    Init->>Reg: Register state_id=42 -> MyState
    Init->>Init: Validate module name
    Init->>GetName: Call cls.get_name() for validation
    GetName->>GetName: Get REFLEX_MINIFY_STATES mode
    alt mode == DISABLED
        GetName->>App: Return full_name (module___class)
    else mode == ENABLED && cls._state_id is not None
        GetName->>Minify: Call _int_to_minified_name(42)
        Minify->>Minify: Convert 42 to base-54 ("q")
        Minify->>GetName: Return "q"
        GetName->>App: Return "q"
    else mode == ENFORCE && cls._state_id is None
        GetName->>App: Raise StateValueError
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

benedikt-bartscher and others added 2 commits January 24, 2026 21:09
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
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