minor performance improvement for _potentially_dirty_substates + tests

This commit is contained in:
Benedikt Bartscher 2024-12-03 02:41:28 +01:00
parent eb23c0e079
commit 6857362618
No known key found for this signature in database
2 changed files with 19 additions and 18 deletions

View File

@ -1899,36 +1899,33 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
if include_backend or not self.computed_vars[cvar]._backend if include_backend or not self.computed_vars[cvar]._backend
) )
# TODO: just return full name? cache?
@classmethod @classmethod
def _potentially_dirty_substates(cls) -> set[Type[BaseState]]: def _potentially_dirty_substates(cls) -> set[str]:
"""Determine substates which could be affected by dirty vars in this state. """Determine substates which could be affected by dirty vars in this state.
Returns: Returns:
Set of State classes that may need to be fetched to recalc computed vars. Set of State full names that may need to be fetched to recalc computed vars.
""" """
# _always_dirty_substates need to be fetched to recalc computed vars. # _always_dirty_substates need to be fetched to recalc computed vars.
fetch_substates = set( fetch_substates = set(
cls.get_class_substate((cls.get_name(), *substate_name.split("."))) f"{cls.get_full_name()}.{substate_name}"
for substate_name in cls._always_dirty_substates for substate_name in cls._always_dirty_substates
) )
for dependent_substates in cls._substate_var_dependencies.values(): for dependent_substates in cls._substate_var_dependencies.values():
fetch_substates.update( fetch_substates.update(
set( set(
cls.get_class_substate((cls.get_name(), *substate_name.split("."))) f"{cls.get_full_name()}.{substate_name}"
for substate_name in dependent_substates for substate_name in dependent_substates
) )
) )
return fetch_substates return fetch_substates
# TODO: just return full name? cache?
# this only needs to be computed once, and only for the root state?
@classmethod @classmethod
def _recursive_potentially_dirty_substates(cls) -> set[Type[BaseState]]: def _recursive_potentially_dirty_substates(cls) -> set[str]:
"""Recursively determine substates which could be affected by dirty vars in this state. """Recursively determine substates which could be affected by dirty vars in this state.
Returns: Returns:
Set of State classes that may need to be fetched to recalc computed vars. Set of full state names that may need to be fetched to recalc computed vars.
""" """
fetch_substates = cls._potentially_dirty_substates() fetch_substates = cls._potentially_dirty_substates()
for substate_cls in cls.get_substates(): for substate_cls in cls.get_substates():
@ -3285,12 +3282,7 @@ class StateManagerRedis(StateManager):
walk_state_path = walk_state_path.rpartition(".")[0] walk_state_path = walk_state_path.rpartition(".")[0]
state_tokens.add(walk_state_path) state_tokens.add(walk_state_path)
state_tokens.update( state_tokens.update(self.state._recursive_potentially_dirty_substates())
{
substate.get_full_name()
for substate in self.state._recursive_potentially_dirty_substates()
}
)
if get_substates: if get_substates:
state_tokens.update( state_tokens.update(
{ {

View File

@ -3135,10 +3135,17 @@ def test_potentially_dirty_substates():
def bar(self) -> str: def bar(self) -> str:
return "" return ""
assert RxState._potentially_dirty_substates() == {State} assert RxState._potentially_dirty_substates() == {State.get_full_name()}
assert State._potentially_dirty_substates() == {C1} assert State._potentially_dirty_substates() == {C1.get_full_name()}
assert C1._potentially_dirty_substates() == set() assert C1._potentially_dirty_substates() == set()
assert RxState._recursive_potentially_dirty_substates() == {
State.get_full_name(),
C1.get_full_name(),
}
assert State._recursive_potentially_dirty_substates() == {C1.get_full_name()}
assert C1._recursive_potentially_dirty_substates() == set()
def test_router_var_dep() -> None: def test_router_var_dep() -> None:
"""Test that router var dependencies are correctly tracked.""" """Test that router var dependencies are correctly tracked."""
@ -3159,7 +3166,9 @@ def test_router_var_dep() -> None:
State._init_var_dependency_dicts() State._init_var_dependency_dicts()
assert foo._deps(objclass=RouterVarDepState) == {"router"} assert foo._deps(objclass=RouterVarDepState) == {"router"}
assert RouterVarParentState._potentially_dirty_substates() == {RouterVarDepState} assert RouterVarParentState._potentially_dirty_substates() == {
RouterVarDepState.get_full_name()
}
assert RouterVarParentState._substate_var_dependencies == { assert RouterVarParentState._substate_var_dependencies == {
"router": {RouterVarDepState.get_name()} "router": {RouterVarDepState.get_name()}
} }