Skip to content

Commit

Permalink
Avoid uncecessary iteration in SequenceLearner (#380)
Browse files Browse the repository at this point in the history
This change is relevant when a SequenceLearner is used with an object that
provides the Sequence interface, but lazily evaluates its elements.
  • Loading branch information
jbweston authored Oct 14, 2022
1 parent e06b34c commit 5ce6da5
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion adaptive/learner/sequence_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class SequenceLearner(BaseLearner):
def __init__(self, function, sequence):
self._original_function = function
self.function = _IgnoreFirstArgument(function)
self._to_do_indices = SortedSet({i for i, _ in enumerate(sequence)})
# prefer range(len(...)) over enumerate to avoid slowdowns
# when passing lazy sequences
self._to_do_indices = SortedSet(range(len(sequence)))
self._ntotal = len(sequence)
self.sequence = copy(sequence)
self.data = SortedDict()
Expand Down

0 comments on commit 5ce6da5

Please sign in to comment.