From 4f4d3f7ec66450b7ad42395bc618d79e82a8cd13 Mon Sep 17 00:00:00 2001 From: NotAFile Date: Thu, 9 Jun 2022 22:50:08 +0200 Subject: [PATCH 1/3] fs: document performance footgun --- tokio/src/fs/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tokio/src/fs/mod.rs b/tokio/src/fs/mod.rs index ca0264b3678..e2f1185d89f 100644 --- a/tokio/src/fs/mod.rs +++ b/tokio/src/fs/mod.rs @@ -21,6 +21,23 @@ //! return `std::io::Result`. Be warned that these adapted methods may return //! `std::io::ErrorKind::WouldBlock` if a *worker* thread can not be converted //! to a *backup* thread immediately. +//! +//! **Warning**: These adapters may create a large number of temporary tasks, +//! especially when reading large files. When performing a lot of operations +//! in one batch, it may be significantly faster to use [`spawn_blocking`] +//! directly: +//! +//! ``` +//! use tokio::fs::File; +//! use std::io::{BufReader, BufRead}; +//! async fn count_lines(file: File) -> Result { +//! let file = file.into_std().await; +//! tokio::task::spawn_blocking(move || { +//! let line_count = BufReader::new(file).lines().count(); +//! Ok(line_count) +//! }).await? +//! } +//! ``` //! //! [`AsyncRead`]: trait@crate::io::AsyncRead From fd54aa5b34e1f3c4206cd123517c7d2f81e759fb Mon Sep 17 00:00:00 2001 From: NotAFile Date: Thu, 9 Jun 2022 23:13:45 +0200 Subject: [PATCH 2/3] fs: add missing doc link --- tokio/src/fs/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tokio/src/fs/mod.rs b/tokio/src/fs/mod.rs index e2f1185d89f..51ab6a70dae 100644 --- a/tokio/src/fs/mod.rs +++ b/tokio/src/fs/mod.rs @@ -39,6 +39,7 @@ //! } //! ``` //! +//! [`spawn_blocking`]: fn@crate::task::spawn_blocking //! [`AsyncRead`]: trait@crate::io::AsyncRead mod canonicalize; From daccfceb67671e5553fb53a1cbdf2739f6233404 Mon Sep 17 00:00:00 2001 From: NotAFile Date: Fri, 10 Jun 2022 12:47:48 +0200 Subject: [PATCH 3/3] fs: fix rustfmt --- tokio/src/fs/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/fs/mod.rs b/tokio/src/fs/mod.rs index 51ab6a70dae..3afefc6e3fb 100644 --- a/tokio/src/fs/mod.rs +++ b/tokio/src/fs/mod.rs @@ -21,7 +21,7 @@ //! return `std::io::Result`. Be warned that these adapted methods may return //! `std::io::ErrorKind::WouldBlock` if a *worker* thread can not be converted //! to a *backup* thread immediately. -//! +//! //! **Warning**: These adapters may create a large number of temporary tasks, //! especially when reading large files. When performing a lot of operations //! in one batch, it may be significantly faster to use [`spawn_blocking`]