improve delta perf

Merged
BE
10d ago
+137-2

Summary

  • build_delta and BaseState.dict() read a field that is stored on the state straight from the instance __dict__, instead of calling get_value() for every var.
  • A computed var, or a field whose default was never stored, still goes through get_value().
  • The result is the same. For a field declared on the state's own class, get_value() reads the same __dict__ entry. A field inherited from a parent state is stored on the parent instance, so it is not in the child's __dict__ and takes the old path.

Measurement

  • BaseState.dict() on a state with 20 fields: 5.3 → 2.85 µs.
  • dict() on a tree of 20 substates: 27.9 → 20.4 µs.
  • CodSpeed: test_hydration_snapshot[200] +35%, test_hydration_snapshot[20] +30%, test_computed_dependency_fanout[0] +10%.

Test plan

  • test_build_delta_reads_stored_fields_from_instance_dict, test_build_delta_stores_default_of_unread_field and test_build_delta_sends_inherited_var_under_its_owner cover the fast path, the default and the inherited fallback in build_delta
  • test_dict_reads_stored_fields_from_instance_dict covers BaseState.dict()
  • uv run pytest tests/units
  • ruff check, ruff format --check, pyright

🤖 Generated with Claude Code