Skip to content

Core: Better scaling explicit indirect conditions #4582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions BaseClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,11 @@ def _update_reachable_regions_explicit_indirect_conditions(self, player: int, qu
self.path[new_region] = (new_region.name, self.path.get(connection, None))

# Retry connections if the new region can unblock them
for new_entrance in self.multiworld.indirect_connections.get(new_region, set()):
if new_entrance in blocked_connections and new_entrance not in queue:
queue.append(new_entrance)
entrances = self.multiworld.indirect_connections.get(new_region)
if entrances is not None:
relevant_entrances = entrances.intersection(blocked_connections)
relevant_entrances.difference_update(queue)
queue.extend(relevant_entrances)
Comment on lines +779 to +783
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
entrances = self.multiworld.indirect_connections.get(new_region)
if entrances is not None:
relevant_entrances = entrances.intersection(blocked_connections)
relevant_entrances.difference_update(queue)
queue.extend(relevant_entrances)
try:
entrances = self.multiworld.indirect_connections[new_region]
relevant_entrances = entrances.intersection(blocked_connections)
relevant_entrances.difference_update(queue)
queue.extend(relevant_entrances)
except KeyError:
pass

is this faster? my understanding is dict.get() essentially does this same thing so this just skips the is not None check

Copy link
Contributor Author

@Mysteryem Mysteryem Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, catching exceptions is slow. Exceptions are not meant to be a default codepath, they are for handling exceptional circumstances.

If the expectation was that almost all regions would be present in self.multiworld.indirect_connections, and that a region not being present was an 'exceptional circumstance', then the try-except could be faster. It really just depends on what percentage of the time you are expecting to have to catch an exception.

My understanding of dict.get() is that it is pretty close to my_dict[key] if key in my_dict else None, but slightly faster for types with more complex, non-cached hashing because dict.get() should only need to get the hash of key once when the key is present in the dictionary, whereas my_dict[key] if key in my_dict else None has to hash key twice when the key is present.


def _update_reachable_regions_auto_indirect_conditions(self, player: int, queue: deque):
reachable_regions = self.reachable_regions[player]
Expand Down
Loading