SM: Speed up deepcopy in copy_mixin (#4228)

This commit is contained in:
Mysteryem
2025-07-30 03:10:36 +01:00
committed by GitHub
parent 2a0ed7faa2
commit 1d8a0b2940
2 changed files with 52 additions and 0 deletions

View File

@@ -66,6 +66,14 @@ class SMBool:
def __copy__(self):
return SMBool(self.bool, self.difficulty, self._knows, self._items)
def __deepcopy__(self, memodict):
# `bool` and `difficulty` are a `bool` and `int`, so do not need to be copied.
# The `_knows` list is never mutated, so does not need to be copied.
# The `_items` list is a `list[str | list[str]]` (copied to a flat `list[str]` when accessed through the `items`
# property) that is mutated by code in helpers.py, so needs to be copied. Because there could be lists within
# the list, it is copied using the `flatten()` helper function.
return SMBool(self.bool, self.difficulty, self._knows, flatten(self._items))
def json(self):
# as we have slots instead of dict
return {'bool': self.bool, 'difficulty': self.difficulty, 'knows': self.knows, 'items': self.items}