-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: spawn jsr futures on executor (#395)
- Loading branch information
1 parent
55425f0
commit fad142c
Showing
7 changed files
with
135 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
use futures::channel::oneshot; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::Context; | ||
use std::task::Poll; | ||
|
||
pub type BoxedFuture = Pin<Box<dyn Future<Output = ()> + 'static>>; | ||
|
||
/// An executor for futures. | ||
/// | ||
/// This trait allows deno_graph to run background tasks on | ||
/// the async executor. | ||
pub trait Executor { | ||
/// Spawns a future to run on this executor. | ||
fn execute(&self, fut: BoxedFuture) -> BoxedFuture; | ||
} | ||
|
||
impl<'a> Default for &'a dyn Executor { | ||
fn default() -> &'a dyn Executor { | ||
{ | ||
struct DefaultExecutor; | ||
|
||
impl Executor for DefaultExecutor { | ||
fn execute(&self, future: BoxedFuture) -> BoxedFuture { | ||
#[cfg(not(feature = "tokio_executor"))] | ||
return future; | ||
|
||
#[cfg(feature = "tokio_executor")] | ||
Box::pin(async { deno_unsync::spawn(future).await.unwrap() }) | ||
} | ||
} | ||
|
||
&DefaultExecutor | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct JoinHandle<T> { | ||
rx: oneshot::Receiver<T>, | ||
fut: BoxedFuture, | ||
} | ||
|
||
impl<T> Future for JoinHandle<T> { | ||
type Output = T; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { | ||
if let Poll::Ready(()) = Pin::new(&mut self.fut).poll(cx) { | ||
if let Poll::Ready(Ok(res)) = Pin::new(&mut self.rx).poll(cx) { | ||
Poll::Ready(res) | ||
} else { | ||
panic!("task panic"); | ||
} | ||
} else { | ||
Poll::Pending | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn spawn<F, T: 'static>( | ||
executor: &dyn Executor, | ||
f: F, | ||
) -> JoinHandle<T> | ||
where | ||
F: Future<Output = T> + 'static, | ||
{ | ||
let (tx, rx) = oneshot::channel(); | ||
let fut = executor.execute(Box::pin(async move { | ||
tx.send(f.await).ok(); | ||
})); | ||
|
||
JoinHandle { rx, fut } | ||
} |