Skip to content

Commit

Permalink
Stardew Valley: cache simplified item rules
Browse files Browse the repository at this point in the history
giving 33% speedup in set_rules
  • Loading branch information
black-sliver committed Oct 28, 2023
1 parent 88d69db commit ac4131a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion worlds/stardew_valley/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,28 @@
fishing_regions = [Region.beach, Region.town, Region.forest, Region.mountain, Region.island_south, Region.island_west]


class SimplifyingDict(dict):
_simplified: Set[str]

def __init__(self, *args):
super().__init__(*args)
self._simplified = set()

def __getitem__(self, item: str) -> StardewRule:
res = super().__getitem__(item)
if item not in self._simplified:
self._simplified.add(item)
res = res.simplify()
self[item] = res
return res


@dataclass(frozen=True, repr=False)
class StardewLogic:
player: int
options: StardewValleyOptions

item_rules: Dict[str, StardewRule] = field(default_factory=dict)
item_rules: Dict[str, StardewRule] = field(default_factory=SimplifyingDict)
sapling_rules: Dict[str, StardewRule] = field(default_factory=dict)
tree_fruit_rules: Dict[str, StardewRule] = field(default_factory=dict)
seed_rules: Dict[str, StardewRule] = field(default_factory=dict)
Expand Down
2 changes: 1 addition & 1 deletion worlds/stardew_valley/stardew_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,4 @@ def __hash__(self):
return hash(self.item)

def simplify(self) -> StardewRule:
return self.other_rules[self.item].simplify()
return self.other_rules[self.item]

0 comments on commit ac4131a

Please sign in to comment.