Skip to content

Commit 63bc62b

Browse files
committed
perf: avoid intermediate OrderedSet allocations in _prepare_entry_states
Replace two set operations (- and |) that each allocate an intermediate OrderedSet with a single-pass generator + update. Eliminates 2 allocations per microstep.
1 parent d6ebb61 commit 63bc62b

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

statemachine/engines/base.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,12 @@ def _prepare_entry_states(
560560

561561
states_targets_to_enter = OrderedSet(info.state for info in ordered_states if info.state)
562562

563-
new_configuration = cast(
564-
OrderedSet[State], (previous_configuration - states_to_exit) | states_targets_to_enter
563+
# Build new configuration in a single pass instead of two set operations
564+
# (- and |) that each allocate an intermediate OrderedSet.
565+
new_configuration = OrderedSet(
566+
s for s in previous_configuration if s not in states_to_exit
565567
)
568+
new_configuration.update(states_targets_to_enter)
566569
self._debug("%s States to enter: %s", self._log_id, states_targets_to_enter)
567570

568571
return ordered_states, states_for_default_entry, default_history_content, new_configuration

0 commit comments

Comments
 (0)