Skip to content

Commit

Permalink
Optimize readiness handling (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 authored Nov 4, 2024
1 parent c26b336 commit ddbc4a7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
6 changes: 5 additions & 1 deletion ntex-service/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changes

## [3.3.0] - 2024-11-02
## [3.3.1] - 2024-11-04

* Optimize readiness handling

## [3.3.0] - 2024-11-04

* Added Service::not_ready() method

Expand Down
2 changes: 1 addition & 1 deletion ntex-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ntex-service"
version = "3.3.0"
version = "3.3.1"
authors = ["ntex contributors <team@ntex.rs>"]
description = "ntex service"
keywords = ["network", "framework", "async", "futures"]
Expand Down
21 changes: 14 additions & 7 deletions ntex-service/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ impl WaitersRef {
}

pub(crate) fn register(&self, idx: u32, cx: &mut Context<'_>) {
let wakers = self.get_wakers();
if let Some(last) = wakers.last() {
if idx == *last {
return;
}
}
wakers.push(idx);
self.get()[idx as usize] = Some(cx.waker().clone());
self.get_wakers().push(idx);
}

pub(crate) fn register_unready(&self, cx: &mut Context<'_>) {
Expand All @@ -66,13 +72,14 @@ impl WaitersRef {
}

pub(crate) fn notify(&self) {
let indexes = self.get();
let wakers = self.get_wakers();

for idx in wakers.drain(..) {
if let Some(item) = indexes.get_mut(idx as usize) {
if let Some(waker) = item.take() {
waker.wake();
if !wakers.is_empty() {
let indexes = self.get();
for idx in wakers.drain(..) {
if let Some(item) = indexes.get_mut(idx as usize) {
if let Some(waker) = item.take() {
waker.wake();
}
}
}
}
Expand Down
54 changes: 26 additions & 28 deletions ntex-service/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,20 @@ where
let mut slf = self.as_mut();

if slf.pl.state.waiters.can_check(slf.pl.index, cx) {
if let Some(ref mut fut) = slf.fut {
match unsafe { Pin::new_unchecked(fut) }.poll(cx) {
Poll::Pending => {
slf.pl.state.waiters.register(slf.pl.index, cx);
Poll::Pending
}
Poll::Ready(res) => {
let _ = slf.fut.take();
slf.pl.state.waiters.notify();
Poll::Ready(res)
}
}
} else {
if slf.fut.is_none() {
slf.fut = Some((slf.f)(slf.pl));
self.poll(cx)
}
let fut = slf.fut.as_mut().unwrap();
match unsafe { Pin::new_unchecked(fut) }.poll(cx) {
Poll::Pending => {
slf.pl.state.waiters.register(slf.pl.index, cx);
Poll::Pending
}
Poll::Ready(res) => {
let _ = slf.fut.take();
slf.pl.state.waiters.notify();
Poll::Ready(res)
}
}
} else {
Poll::Pending
Expand All @@ -460,21 +459,20 @@ where
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
let mut slf = self.as_mut();

if let Some(ref mut fut) = slf.fut {
match unsafe { Pin::new_unchecked(fut) }.poll(cx) {
Poll::Pending => {
slf.pl.state.waiters.register_unready(cx);
Poll::Pending
}
Poll::Ready(res) => {
let _ = slf.fut.take();
slf.pl.state.waiters.notify();
Poll::Ready(res)
}
}
} else {
if slf.fut.is_none() {
slf.fut = Some((slf.f)(slf.pl));
self.poll(cx)
}
let fut = slf.fut.as_mut().unwrap();
match unsafe { Pin::new_unchecked(fut) }.poll(cx) {
Poll::Pending => {
slf.pl.state.waiters.register_unready(cx);
Poll::Pending
}
Poll::Ready(res) => {
let _ = slf.fut.take();
slf.pl.state.waiters.notify();
Poll::Ready(res)
}
}
}
}

0 comments on commit ddbc4a7

Please sign in to comment.