Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Trim off finished futures from the Event._waiters #431

Closed
wants to merge 1 commit into from
Closed

Trim off finished futures from the Event._waiters #431

wants to merge 1 commit into from

Conversation

ZhukovAlexander
Copy link

Current implementation of Event.wait uses dequeue.remove(e),
wich has O(n) complexity. We can do better by utilizing this
time to also remove all finished futures from the queue
on the way up until we reach the future that is in a pending
state. This will make subsequent calls to Event.wait or
Event.set to perform less work on a waiters queue

Current implementation of Event.wait uses dequeue.remove(e),
wich has O(n) complexity. We can do better by utilizing this
time to also remove all finished futures from the queue
on the way up until we reach the future that is in a pending
state. This will make subsequent calls to Event.wait or
Event.set to perform less work on a waiters queue
if not self._waiters[0].done():
break
else:
self._waiters.popleft()
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to see some kind of proof/assertion that once we fall out of the loop fut has in fact been removed.

Copy link
Author

Choose a reason for hiding this comment

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

Would checking if the poped future is the one we care about and raising a RuntimeError if it wasn't found work?

else:
    found = self._waiters.popleft() is fut
if not found:
    raise RuntimeError

@sethmlarson
Copy link

sethmlarson commented Sep 28, 2016

Pretty sure that fut is always at index 0 within _waiters by the time that it yields. remove() only removes the first encountered element so it'll stop there, so probably no speed-up using this trimming method. Instead of using remove() could just use popleft() as it's probably faster.

@ZhukovAlexander
Copy link
Author

@SethMichaelLarson , I think you are right. Though, the side effect of this algorithm is that it will also trim all finished futures that come after the current one, so it still has an effect.

@sethmlarson
Copy link

@ZhukovAlexander When would that occur? Each future trims itself as it's finished so there's only ever one finished future in _waiters. The only time I can think where that would happen is if code was muddling with the internals.

@gvanrossum
Copy link
Member

If fut always occurs at index 0 there is no O() improvement from using popleft(), so the point of the diff is moot -- let's close this PR then.

@gvanrossum gvanrossum closed this Sep 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants