From 5b63dfdcb6b19ae3f1237d9f6cb3999dec3ebcf1 Mon Sep 17 00:00:00 2001 From: doyoubi Date: Mon, 4 Nov 2019 21:28:42 +0800 Subject: [PATCH] Fix SplitSink performance problem caused by flushing inner sink every time --- futures-util/src/stream/stream/split.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/futures-util/src/stream/stream/split.rs b/futures-util/src/stream/stream/split.rs index 28d7aa71a0..43853edb5f 100644 --- a/futures-util/src/stream/stream/split.rs +++ b/futures-util/src/stream/stream/split.rs @@ -61,6 +61,19 @@ impl + Unpin, Item> SplitSink { } } +impl, Item> SplitSink { + fn poll_flush_slot(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let this = &mut *self; + let mut inner = ready!(this.lock.poll_lock(cx)); + if this.slot.is_some() { + ready!(inner.as_pin_mut().poll_ready(cx))?; + Poll::Ready(inner.as_pin_mut().start_send(this.slot.take().unwrap())) + } else { + Poll::Ready(Ok(())) + } + } +} + impl, Item> Sink for SplitSink { type Error = S::Error; @@ -69,7 +82,7 @@ impl, Item> Sink for SplitSink { if self.slot.is_none() { return Poll::Ready(Ok(())); } - ready!(self.as_mut().poll_flush(cx))?; + ready!(self.as_mut().poll_flush_slot(cx))?; } }