From bb14f92f4188c401839d0f4c86f82c63b545fdf6 Mon Sep 17 00:00:00 2001 From: hardfist Date: Sat, 21 Sep 2024 21:03:41 +0800 Subject: [PATCH] chore: try async fs for better performance --- crates/rspack_loader_runner/src/runner.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/rspack_loader_runner/src/runner.rs b/crates/rspack_loader_runner/src/runner.rs index 5d7f849fe3e..ae0fda05703 100644 --- a/crates/rspack_loader_runner/src/runner.rs +++ b/crates/rspack_loader_runner/src/runner.rs @@ -4,6 +4,7 @@ use rspack_error::{error, IntoTWithDiagnosticArray, Result, TWithDiagnosticArray use rspack_fs::ReadableFileSystem; use rspack_sources::SourceMap; use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; +use tokio::task::spawn_blocking; use crate::{ content::{AdditionalData, Content, ResourceData}, @@ -41,9 +42,12 @@ async fn process_resource( if let Some(resource_path) = resource_data.resource_path.as_deref() && !resource_path.as_str().is_empty() { - let result = fs - .read(resource_path.as_std_path()) - .map_err(|e| error!("{e}, failed to read {resource_path}"))?; + let resource_path_owned = resource_path.to_owned(); + // use spawn_blocking to avoid block,see https://docs.rs/tokio/latest/src/tokio/fs/read.rs.html#48 + let result = spawn_blocking(move || fs.read(resource_path_owned.as_std_path())) + .await + .map_err(|e| error!("{e}, spawn task failed"))?; + let result = result.map_err(|e| error!("{e}, failed to read {resource_path}"))?; loader_context.content = Some(Content::from(result)); } else if !resource_data.get_scheme().is_none() { let resource = &resource_data.resource;