Inline(never) more things for rqbit(bin) to compile faster

This commit is contained in:
Igor Katson 2024-02-26 23:08:47 +00:00
parent f42007f436
commit 34f3ec6c29
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
5 changed files with 72 additions and 47 deletions

View file

@ -45,6 +45,7 @@ impl HttpApi {
/// Run the HTTP server forever on the given address. /// Run the HTTP server forever on the given address.
/// If read_only is passed, no state-modifying methods will be exposed. /// If read_only is passed, no state-modifying methods will be exposed.
#[inline(never)]
pub fn make_http_api_and_run( pub fn make_http_api_and_run(
self, self,
addr: SocketAddr, addr: SocketAddr,

View file

@ -1,4 +1,7 @@
use std::pin::Pin;
use anyhow::Context; use anyhow::Context;
use futures::{Future, FutureExt};
use serde::Deserialize; use serde::Deserialize;
use crate::{ use crate::{
@ -59,6 +62,7 @@ async fn json_response<T: serde::de::DeserializeOwned + std::any::Any>(
} }
impl HttpApiClient { impl HttpApiClient {
#[inline(never)]
pub fn new(url: &str) -> anyhow::Result<Self> { pub fn new(url: &str) -> anyhow::Result<Self> {
Ok(Self { Ok(Self {
base_url: reqwest::Url::parse(url)?, base_url: reqwest::Url::parse(url)?,
@ -70,40 +74,47 @@ impl HttpApiClient {
&self.base_url &self.base_url
} }
pub async fn validate_rqbit_server(&self) -> anyhow::Result<()> { #[inline(never)]
let response = self.client.get(self.base_url.clone()).send().await?; pub fn validate_rqbit_server(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + '_>> {
let root: ApiRoot = json_response(response).await?; async move {
if root.server == "rqbit" { let response = self.client.get(self.base_url.clone()).send().await?;
return Ok(()); 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( pub fn add_torrent<'a>(
&self, &'a self,
torrent: AddTorrent<'_>, torrent: AddTorrent<'a>,
opts: Option<AddTorrentOptions>, opts: Option<AddTorrentOptions>,
) -> anyhow::Result<ApiAddTorrentResponse> { ) -> Pin<Box<dyn Future<Output = anyhow::Result<ApiAddTorrentResponse>> + 'a>> {
let opts = opts.unwrap_or_default(); async move {
let params = TorrentAddQueryParams { let opts = opts.unwrap_or_default();
overwrite: Some(opts.overwrite), let params = TorrentAddQueryParams {
only_files_regex: opts.only_files_regex, overwrite: Some(opts.overwrite),
only_files: None, only_files_regex: opts.only_files_regex,
output_folder: opts.output_folder, only_files: None,
sub_folder: opts.sub_folder, output_folder: opts.output_folder,
list_only: Some(opts.list_only), sub_folder: opts.sub_folder,
..Default::default() list_only: Some(opts.list_only),
}; ..Default::default()
let qs = serde_urlencoded::to_string(&params).unwrap(); };
let url = format!("{}torrents?{}", &self.base_url, qs); let qs = serde_urlencoded::to_string(&params).unwrap();
let response = check_response( let url = format!("{}torrents?{}", &self.base_url, qs);
self.client let response = check_response(
.post(&url) self.client
.body(torrent.into_bytes()) .post(&url)
.send() .body(torrent.into_bytes())
.await?, .send()
) .await?,
.await?; )
json_response(response).await .await?;
json_response(response).await
}
.boxed()
} }
} }

View file

@ -298,6 +298,7 @@ pub enum AddTorrent<'a> {
impl<'a> AddTorrent<'a> { impl<'a> AddTorrent<'a> {
// Don't call this from HTTP API. // Don't call this from HTTP API.
#[inline(never)]
pub fn from_cli_argument(path: &'a str) -> anyhow::Result<Self> { pub fn from_cli_argument(path: &'a str) -> anyhow::Result<Self> {
if SUPPORTED_SCHEMES.iter().any(|s| path.starts_with(s)) { if SUPPORTED_SCHEMES.iter().any(|s| path.starts_with(s)) {
return Ok(Self::Url(Cow::Borrowed(path))); return Ok(Self::Url(Cow::Borrowed(path)));
@ -314,6 +315,7 @@ impl<'a> AddTorrent<'a> {
} }
// Don't call this from HTTP API. // Don't call this from HTTP API.
#[inline(never)]
pub fn from_local_filename(filename: &str) -> anyhow::Result<Self> { pub fn from_local_filename(filename: &str) -> anyhow::Result<Self> {
let file = read_local_file_including_stdin(filename) let file = read_local_file_including_stdin(filename)
.with_context(|| format!("error reading local file {filename:?}"))?; .with_context(|| format!("error reading local file {filename:?}"))?;
@ -744,6 +746,7 @@ impl Session {
} }
/// Add a torrent to the session. /// Add a torrent to the session.
#[inline(never)]
pub fn add_torrent<'a>( pub fn add_torrent<'a>(
&'a self, &'a self,
add: AddTorrent<'a>, add: AddTorrent<'a>,

View file

@ -8,6 +8,7 @@ use std::collections::HashSet;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::pin::Pin;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -15,6 +16,8 @@ use std::time::Duration;
use anyhow::bail; use anyhow::bail;
use anyhow::Context; use anyhow::Context;
use buffers::ByteString; use buffers::ByteString;
use futures::Future;
use futures::FutureExt;
use librqbit_core::hash_id::Id20; use librqbit_core::hash_id::Id20;
use librqbit_core::lengths::Lengths; use librqbit_core::lengths::Lengths;
use librqbit_core::peer_id::generate_peer_id; use librqbit_core::peer_id::generate_peer_id;
@ -395,23 +398,29 @@ impl ManagedTorrent {
}) })
} }
pub async fn wait_until_completed(&self) -> anyhow::Result<()> { #[inline(never)]
// TODO: rewrite, this polling is horrible pub fn wait_until_completed(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + '_>> {
let live = loop { async move {
let live = self.with_state(|s| match s { // TODO: rewrite, this polling is horrible
ManagedTorrentState::Initializing(_) | ManagedTorrentState::Paused(_) => Ok(None), let live = loop {
ManagedTorrentState::Live(l) => Ok(Some(l.clone())), let live = self.with_state(|s| match s {
ManagedTorrentState::Error(e) => bail!("{:?}", e), ManagedTorrentState::Initializing(_) | ManagedTorrentState::Paused(_) => {
ManagedTorrentState::None => bail!("bug: torrent state is None"), Ok(None)
})?; }
if let Some(live) = live { ManagedTorrentState::Live(l) => Ok(Some(l.clone())),
break live; ManagedTorrentState::Error(e) => bail!("{:?}", e),
} ManagedTorrentState::None => bail!("bug: torrent state is None"),
tokio::time::sleep(Duration::from_secs(1)).await; })?;
}; if let Some(live) = live {
break live;
}
tokio::time::sleep(Duration::from_secs(1)).await;
};
live.wait_until_completed().await; live.wait_until_completed().await;
Ok(()) Ok(())
}
.boxed()
} }
} }

View file

@ -159,6 +159,7 @@ impl<BufType: AsRef<[u8]>> TorrentMetaV1Info<BufType> {
Some(expected_hash == hash) Some(expected_hash == hash)
} }
#[inline(never)]
pub fn iter_filenames_and_lengths( pub fn iter_filenames_and_lengths(
&self, &self,
) -> anyhow::Result<impl Iterator<Item = (FileIteratorName<'_, BufType>, u64)>> { ) -> anyhow::Result<impl Iterator<Item = (FileIteratorName<'_, BufType>, u64)>> {