From 97f383cb780e73c7a84c9ae2f2528e87a7401dc2 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Tue, 26 Dec 2023 16:45:08 -0600 Subject: [PATCH] de-async (work ahead of time) --- server/src/mine.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/server/src/mine.rs b/server/src/mine.rs index 69f0aa7..aed76fe 100644 --- a/server/src/mine.rs +++ b/server/src/mine.rs @@ -289,7 +289,7 @@ impl Task for Quarry { return TaskState::Complete; } - if self.progress.allocated().await { + if self.progress.allocated() { return TaskState::Waiting; } @@ -312,8 +312,9 @@ impl Task for Quarry { #[derive(Serialize, Deserialize, Clone)] struct ChunkedTask { confirmed: Arc, + head: Arc, // highest active chunk #[serde(skip)] - in_flight: Arc>>, + in_flight: Arc>>, // must remain sorted max: i32, } @@ -321,6 +322,7 @@ impl ChunkedTask { fn new(parts: i32) -> Self { Self { confirmed: Default::default(), + head: Default::default(), in_flight: Default::default(), max: parts, } @@ -328,32 +330,27 @@ impl ChunkedTask { fn done(&self) -> bool { let backstop = self.confirmed.load(Ordering::SeqCst); - backstop >= self.max + backstop + 1 >= self.max } - async fn allocated(&self) -> bool { - let mut in_flight = self.in_flight.clone().write_owned().await; - in_flight.sort_unstable(); - - let backstop = self.confirmed.load(Ordering::SeqCst); - - for i in backstop..self.max { - if in_flight.get(i as usize).is_none() { - return false; - } - } - - return true; + fn allocated(&self) -> bool { + let front = self.head.load(Ordering::SeqCst); + front + 1 >= self.max } async fn next_chunk(&self) -> Option { let mut in_flight = self.in_flight.clone().write_owned().await; - in_flight.sort_unstable(); let backstop = self.confirmed.load(Ordering::SeqCst); + // we have a mutex anyway + if let Some(highest) = in_flight.last() { + self.head.store(*highest, Ordering::SeqCst); + } + for i in backstop..self.max { if in_flight.get(i as usize).is_none() { in_flight.push(i); + in_flight.sort_unstable(); return Some(i); } } @@ -401,7 +398,7 @@ mod tests { assert_eq!(tracker.next_chunk().await, Some(4)); assert_eq!(tracker.next_chunk().await, None); assert_eq!(tracker.done(), false); - assert_eq!(tracker.allocated().await, true); + assert_eq!(tracker.allocated(), true); tracker.mark_done(0).await; tracker.mark_done(1).await; tracker.mark_done(2).await;