Skip to content

Commit

Permalink
use partial(next, itertools.count()) to generate unique ids
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Apr 22, 2020
1 parent 20be476 commit 2f71937
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions adaptive/runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import abc
import asyncio
import concurrent.futures as concurrent
import functools
import inspect
import itertools
import pickle
import sys
import time
Expand Down Expand Up @@ -150,9 +152,10 @@ def __init__(
self._to_retry = {}
self._tracebacks = {}

# Keeping track of index -> point
self._id_to_point = {}
self._i = 0 # some unique index to be associated with each point
self._next_id = functools.partial(
next, itertools.count()
) # some unique id to be associated with each point

def _get_max_tasks(self):
return self._max_tasks or _get_ncores(self.executor)
Expand All @@ -172,10 +175,10 @@ def do_log(self):

def _ask(self, n):
points = []
for i, index in enumerate(self._to_retry.keys()):
for i, _id in enumerate(self._to_retry.keys()):
if i == n:
break
point = self._id_to_point[index]
point = self._id_to_point[_id]
if point not in self.pending_points.values():
points.append(point)

Expand Down Expand Up @@ -249,11 +252,10 @@ def _get_futures(self):
fut.start_time = start_time
self.pending_points[fut] = x
try:
i = _key_by_value(self._id_to_point, x) # O(N)
_id = _key_by_value(self._id_to_point, x) # O(N)
except StopIteration: # `x` is not a value in `self._id_to_point`
self._i += 1
i = self._i
self._id_to_point[i] = x
_id = self._next_id()
self._id_to_point[_id] = x

# Collect and results and add them to the learner
futures = list(self.pending_points.keys())
Expand Down

0 comments on commit 2f71937

Please sign in to comment.