Perform startup/init code eagerly in entry::install_crates (#880)

The startup/init code in `entry::install_crates` performs a lot of blocking operations, from testing if dir exists to reading from files and there is no `.await` in there until after the startup.

There are also a few cases where `block_in_place` should be called (e.g. loading manifests, loading TLS certificates) but is missing.

Most of the `Args` passed to `entry::install_crates` are actually consumed before the first `.await` point, so performing startup/init code eagerly would make the generated future much smaller, reduce codegen and also makes it easier to optimize.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-03-11 15:06:46 +11:00 committed by GitHub
parent 18bc81f46f
commit c5d0b84aa6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 90 deletions

View file

@ -35,29 +35,33 @@ impl Termination for MainExit {
}
impl MainExit {
pub fn new(result: Result<Result<()>, BinstallError>, done: Duration) -> Self {
result.map_or_else(MainExit::Error, |res| {
res.map(|()| MainExit::Success(Some(done)))
.unwrap_or_else(|err| {
err.downcast::<BinstallError>()
.map(MainExit::Error)
.unwrap_or_else(MainExit::Report)
})
})
pub fn new(res: Result<()>, done: Duration) -> Self {
res.map(|()| MainExit::Success(Some(done)))
.unwrap_or_else(|err| {
err.downcast::<BinstallError>()
.map(MainExit::Error)
.unwrap_or_else(MainExit::Report)
})
}
}
/// This function would start a tokio multithreading runtime,
/// spawn a new task on it that runs `f`, then `block_on` it.
/// spawn a new task on it that runs `f()`, then `block_on` it.
///
/// It will cancel the future if user requested cancellation
/// via signal.
pub fn run_tokio_main<F, T>(f: F) -> Result<T, BinstallError>
pub fn run_tokio_main<Func, Fut>(f: Func) -> Result<()>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
Func: FnOnce() -> Result<Option<Fut>>,
Fut: Future<Output = Result<()>> + Send + 'static,
{
let rt = Runtime::new()?;
let handle = AutoAbortJoinHandle::new(rt.spawn(f));
rt.block_on(cancel_on_user_sig_term(handle))
let rt = Runtime::new().map_err(BinstallError::from)?;
let _guard = rt.enter();
if let Some(fut) = f()? {
let handle = AutoAbortJoinHandle::new(rt.spawn(fut));
rt.block_on(cancel_on_user_sig_term(handle))?
} else {
Ok(())
}
}