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,7 +74,9 @@ impl HttpApiClient {
&self.base_url &self.base_url
} }
pub async fn validate_rqbit_server(&self) -> anyhow::Result<()> { #[inline(never)]
pub fn validate_rqbit_server(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + '_>> {
async move {
let response = self.client.get(self.base_url.clone()).send().await?; let response = self.client.get(self.base_url.clone()).send().await?;
let root: ApiRoot = json_response(response).await?; let root: ApiRoot = json_response(response).await?;
if root.server == "rqbit" { if root.server == "rqbit" {
@ -78,12 +84,15 @@ impl HttpApiClient {
} }
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>> {
async move {
let opts = opts.unwrap_or_default(); let opts = opts.unwrap_or_default();
let params = TorrentAddQueryParams { let params = TorrentAddQueryParams {
overwrite: Some(opts.overwrite), overwrite: Some(opts.overwrite),
@ -106,4 +115,6 @@ impl HttpApiClient {
.await?; .await?;
json_response(response).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,11 +398,15 @@ impl ManagedTorrent {
}) })
} }
pub async fn wait_until_completed(&self) -> anyhow::Result<()> { #[inline(never)]
pub fn wait_until_completed(&self) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + '_>> {
async move {
// TODO: rewrite, this polling is horrible // TODO: rewrite, this polling is horrible
let live = loop { let live = loop {
let live = self.with_state(|s| match s { let live = self.with_state(|s| match s {
ManagedTorrentState::Initializing(_) | ManagedTorrentState::Paused(_) => Ok(None), ManagedTorrentState::Initializing(_) | ManagedTorrentState::Paused(_) => {
Ok(None)
}
ManagedTorrentState::Live(l) => Ok(Some(l.clone())), ManagedTorrentState::Live(l) => Ok(Some(l.clone())),
ManagedTorrentState::Error(e) => bail!("{:?}", e), ManagedTorrentState::Error(e) => bail!("{:?}", e),
ManagedTorrentState::None => bail!("bug: torrent state is None"), ManagedTorrentState::None => bail!("bug: torrent state is None"),
@ -413,6 +420,8 @@ impl ManagedTorrent {
live.wait_until_completed().await; live.wait_until_completed().await;
Ok(()) Ok(())
} }
.boxed()
}
} }
pub struct ManagedTorrentBuilder { pub struct ManagedTorrentBuilder {

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)>> {