Skip to content

Commit 8d17ba9

Browse files
committed
perf: eliminate allocation overhead in Configuration.add()/discard()
Mutate the OrderedSet in place and write back via the setter instead of creating a new OrderedSet on every call. This removes the ~4-5% overhead introduced by the persistence bugfix while preserving correctness for both plain-attribute and deserializing-property models.
1 parent cd05c6e commit 8d17ba9

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

statemachine/configuration.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,22 @@ def add(self, state: "State"):
106106
if csv is None:
107107
self.value = state.value
108108
elif isinstance(csv, MutableSet):
109-
new = OrderedSet(csv)
110-
new.add(state.value)
111-
self.value = new
109+
csv.add(state.value)
110+
self.value = csv
112111
else:
113112
self.value = OrderedSet([csv, state.value])
114113

115114
def discard(self, state: "State"):
116115
"""Remove *state* from the configuration, normalizing back to scalar."""
117116
csv = self.value
118117
if isinstance(csv, MutableSet):
119-
new = OrderedSet(v for v in csv if v != state.value)
120-
if len(new) == 0:
118+
csv.discard(state.value)
119+
if len(csv) == 0:
121120
self.value = None
122-
elif len(new) == 1:
123-
self.value = next(iter(new))
121+
elif len(csv) == 1:
122+
self.value = next(iter(csv))
124123
else:
125-
self.value = new
124+
self.value = csv
126125
elif csv == state.value:
127126
self.value = None
128127

0 commit comments

Comments
 (0)