From 68005661d9b83ab59b9894b516f823d41a29e0dc Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Wed, 27 Nov 2019 15:47:27 +0100 Subject: [PATCH] fix Stream::throttle hot loop (#584) Signed-off-by: Yoshua Wuyts --- src/stream/stream/mod.rs | 23 +++++++---------------- src/stream/stream/throttle.rs | 5 +---- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index e68e6acd..f7a1ba2c 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -336,28 +336,19 @@ extension_trait! { let start = Instant::now(); // emit value every 5 milliseconds - let s = stream::interval(Duration::from_millis(5)) - .enumerate() - .take(3); + let s = stream::interval(Duration::from_millis(5)).take(2); // throttle for 10 milliseconds let mut s = s.throttle(Duration::from_millis(10)); - assert_eq!(s.next().await, Some((0, ()))); - let duration_ms = start.elapsed().as_millis(); - assert!(duration_ms >= 5); + s.next().await; + assert!(start.elapsed().as_millis() >= 5); - assert_eq!(s.next().await, Some((1, ()))); - let duration_ms = start.elapsed().as_millis(); - assert!(duration_ms >= 15); + s.next().await; + assert!(start.elapsed().as_millis() >= 15); - assert_eq!(s.next().await, Some((2, ()))); - let duration_ms = start.elapsed().as_millis(); - assert!(duration_ms >= 25); - - assert_eq!(s.next().await, None); - let duration_ms = start.elapsed().as_millis(); - assert!(duration_ms >= 35); + s.next().await; + assert!(start.elapsed().as_millis() >= 35); # # }) } ``` diff --git a/src/stream/stream/throttle.rs b/src/stream/stream/throttle.rs index b2480bbd..ce8c13b3 100644 --- a/src/stream/stream/throttle.rs +++ b/src/stream/stream/throttle.rs @@ -55,10 +55,7 @@ impl Stream for Throttle { } match this.stream.poll_next(cx) { - Poll::Pending => { - cx.waker().wake_by_ref(); // Continue driving even though emitting Pending - Poll::Pending - } + Poll::Pending => Poll::Pending, Poll::Ready(None) => Poll::Ready(None), Poll::Ready(Some(v)) => { *this.blocked = true;