From 34f3ec6c2903298d0e17a65c7a26e38792692478 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 26 Feb 2024 23:08:47 +0000 Subject: [PATCH] Inline(never) more things for rqbit(bin) to compile faster --- crates/librqbit/src/http_api.rs | 1 + crates/librqbit/src/http_api_client.rs | 73 +++++++++++--------- crates/librqbit/src/session.rs | 3 + crates/librqbit/src/torrent_state/mod.rs | 41 ++++++----- crates/librqbit_core/src/torrent_metainfo.rs | 1 + 5 files changed, 72 insertions(+), 47 deletions(-) diff --git a/crates/librqbit/src/http_api.rs b/crates/librqbit/src/http_api.rs index a2f1abe..806ba4c 100644 --- a/crates/librqbit/src/http_api.rs +++ b/crates/librqbit/src/http_api.rs @@ -45,6 +45,7 @@ impl HttpApi { /// Run the HTTP server forever on the given address. /// If read_only is passed, no state-modifying methods will be exposed. + #[inline(never)] pub fn make_http_api_and_run( self, addr: SocketAddr, diff --git a/crates/librqbit/src/http_api_client.rs b/crates/librqbit/src/http_api_client.rs index 2b6bf77..a1bfb47 100644 --- a/crates/librqbit/src/http_api_client.rs +++ b/crates/librqbit/src/http_api_client.rs @@ -1,4 +1,7 @@ +use std::pin::Pin; + use anyhow::Context; +use futures::{Future, FutureExt}; use serde::Deserialize; use crate::{ @@ -59,6 +62,7 @@ async fn json_response( } impl HttpApiClient { + #[inline(never)] pub fn new(url: &str) -> anyhow::Result { Ok(Self { base_url: reqwest::Url::parse(url)?, @@ -70,40 +74,47 @@ impl HttpApiClient { &self.base_url } - pub async fn validate_rqbit_server(&self) -> anyhow::Result<()> { - let response = self.client.get(self.base_url.clone()).send().await?; - let root: ApiRoot = json_response(response).await?; - if root.server == "rqbit" { - return Ok(()); + #[inline(never)] + pub fn validate_rqbit_server(&self) -> Pin> + '_>> { + async move { + let response = self.client.get(self.base_url.clone()).send().await?; + let root: ApiRoot = json_response(response).await?; + if root.server == "rqbit" { + return Ok(()); + } + anyhow::bail!("not an rqbit server at {}", &self.base_url) } - anyhow::bail!("not an rqbit server at {}", &self.base_url) + .boxed() } - pub async fn add_torrent( - &self, - torrent: AddTorrent<'_>, + pub fn add_torrent<'a>( + &'a self, + torrent: AddTorrent<'a>, opts: Option, - ) -> anyhow::Result { - let opts = opts.unwrap_or_default(); - let params = TorrentAddQueryParams { - overwrite: Some(opts.overwrite), - only_files_regex: opts.only_files_regex, - only_files: None, - output_folder: opts.output_folder, - sub_folder: opts.sub_folder, - list_only: Some(opts.list_only), - ..Default::default() - }; - let qs = serde_urlencoded::to_string(¶ms).unwrap(); - let url = format!("{}torrents?{}", &self.base_url, qs); - let response = check_response( - self.client - .post(&url) - .body(torrent.into_bytes()) - .send() - .await?, - ) - .await?; - json_response(response).await + ) -> Pin> + 'a>> { + async move { + let opts = opts.unwrap_or_default(); + let params = TorrentAddQueryParams { + overwrite: Some(opts.overwrite), + only_files_regex: opts.only_files_regex, + only_files: None, + output_folder: opts.output_folder, + sub_folder: opts.sub_folder, + list_only: Some(opts.list_only), + ..Default::default() + }; + let qs = serde_urlencoded::to_string(¶ms).unwrap(); + let url = format!("{}torrents?{}", &self.base_url, qs); + let response = check_response( + self.client + .post(&url) + .body(torrent.into_bytes()) + .send() + .await?, + ) + .await?; + json_response(response).await + } + .boxed() } } diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index 1641a14..9ee814d 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -298,6 +298,7 @@ pub enum AddTorrent<'a> { impl<'a> AddTorrent<'a> { // Don't call this from HTTP API. + #[inline(never)] pub fn from_cli_argument(path: &'a str) -> anyhow::Result { if SUPPORTED_SCHEMES.iter().any(|s| path.starts_with(s)) { return Ok(Self::Url(Cow::Borrowed(path))); @@ -314,6 +315,7 @@ impl<'a> AddTorrent<'a> { } // Don't call this from HTTP API. + #[inline(never)] pub fn from_local_filename(filename: &str) -> anyhow::Result { let file = read_local_file_including_stdin(filename) .with_context(|| format!("error reading local file {filename:?}"))?; @@ -744,6 +746,7 @@ impl Session { } /// Add a torrent to the session. + #[inline(never)] pub fn add_torrent<'a>( &'a self, add: AddTorrent<'a>, diff --git a/crates/librqbit/src/torrent_state/mod.rs b/crates/librqbit/src/torrent_state/mod.rs index 90bfd8e..1e795de 100644 --- a/crates/librqbit/src/torrent_state/mod.rs +++ b/crates/librqbit/src/torrent_state/mod.rs @@ -8,6 +8,7 @@ use std::collections::HashSet; use std::net::SocketAddr; use std::path::Path; use std::path::PathBuf; +use std::pin::Pin; use std::sync::atomic::Ordering; use std::sync::Arc; use std::time::Duration; @@ -15,6 +16,8 @@ use std::time::Duration; use anyhow::bail; use anyhow::Context; use buffers::ByteString; +use futures::Future; +use futures::FutureExt; use librqbit_core::hash_id::Id20; use librqbit_core::lengths::Lengths; use librqbit_core::peer_id::generate_peer_id; @@ -395,23 +398,29 @@ impl ManagedTorrent { }) } - pub async fn wait_until_completed(&self) -> anyhow::Result<()> { - // TODO: rewrite, this polling is horrible - let live = loop { - let live = self.with_state(|s| match s { - ManagedTorrentState::Initializing(_) | ManagedTorrentState::Paused(_) => Ok(None), - ManagedTorrentState::Live(l) => Ok(Some(l.clone())), - ManagedTorrentState::Error(e) => bail!("{:?}", e), - ManagedTorrentState::None => bail!("bug: torrent state is None"), - })?; - if let Some(live) = live { - break live; - } - tokio::time::sleep(Duration::from_secs(1)).await; - }; + #[inline(never)] + pub fn wait_until_completed(&self) -> Pin> + '_>> { + async move { + // TODO: rewrite, this polling is horrible + let live = loop { + let live = self.with_state(|s| match s { + ManagedTorrentState::Initializing(_) | ManagedTorrentState::Paused(_) => { + Ok(None) + } + ManagedTorrentState::Live(l) => Ok(Some(l.clone())), + ManagedTorrentState::Error(e) => bail!("{:?}", e), + ManagedTorrentState::None => bail!("bug: torrent state is None"), + })?; + if let Some(live) = live { + break live; + } + tokio::time::sleep(Duration::from_secs(1)).await; + }; - live.wait_until_completed().await; - Ok(()) + live.wait_until_completed().await; + Ok(()) + } + .boxed() } } diff --git a/crates/librqbit_core/src/torrent_metainfo.rs b/crates/librqbit_core/src/torrent_metainfo.rs index c89230e..e60fcee 100644 --- a/crates/librqbit_core/src/torrent_metainfo.rs +++ b/crates/librqbit_core/src/torrent_metainfo.rs @@ -159,6 +159,7 @@ impl> TorrentMetaV1Info { Some(expected_hash == hash) } + #[inline(never)] pub fn iter_filenames_and_lengths( &self, ) -> anyhow::Result, u64)>> {