Skip to content

Commit e56ff56

Browse files
committed
Switch to itertools chaining from repeated updates in reversed follow
1 parent 3345075 commit e56ff56

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

interegular/fsm.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections import defaultdict
66
from functools import total_ordering
77
from typing import Any, Set, Dict, Union, NewType, Mapping, Tuple, Iterable, Callable, List
8+
from itertools import chain
89

910
from interegular.utils import soft_repr
1011

@@ -578,12 +579,17 @@ def reversed(self):
578579
# Find every possible way to reach the current state-set
579580
# using this symbol.
580581
def follow(current, transition):
581-
next_states = set()
582-
for state in current:
583-
next_states.update(reverse_map.get((state, transition), set()))
582+
_empty_set = set() # reuse to avoid unnecessary allocations
583+
584+
next_states_iter = (
585+
reverse_map.get((state, transition), _empty_set)
586+
for state in current
587+
)
588+
next_states = frozenset(chain.from_iterable(next_states_iter))
589+
584590
if not next_states:
585591
raise OblivionError
586-
return frozenset(next_states)
592+
return next_states
587593

588594
# A state-set is final if the initial state is in it.
589595
def final(state):

0 commit comments

Comments
 (0)