Skip to content

Commit

Permalink
Optimize mapmerge's DMM.generate_new_key method (#56068)
Browse files Browse the repository at this point in the history
Tested on #55867, takes the merge driver's runtime from 148.1s to 4.8s
  • Loading branch information
SpaceManiac committed Jan 11, 2021
1 parent 79a9cec commit be987fc
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions tools/mapmerge2/dmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ def set_tile(self, coord, tile):

def generate_new_key(self):
free_keys = self._ensure_free_keys(1)
max_key = max_key_for(self.key_length)
# choose one of the free keys at random
key = 0
while free_keys:
if key not in self.dictionary:
# this construction is used to avoid needing to construct the
# full set in order to random.choice() from it
if random.random() < 1 / free_keys:
return key
free_keys -= 1
key += 1

raise RuntimeError("ran out of keys, this shouldn't happen")
key = random.randint(0, max_key - 1)
while key in self.dictionary:
key = random.randint(0, max_key - 1)
return key

def overwrite_key(self, key, fixed, bad_keys):
try:
Expand Down

0 comments on commit be987fc

Please sign in to comment.