Skip to content

Commit

Permalink
chore: try async fs for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Sep 21, 2024
1 parent 3ea1fe4 commit bb14f92
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions crates/rspack_loader_runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -41,9 +42,12 @@ async fn process_resource<Context: Send>(
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;
Expand Down

0 comments on commit bb14f92

Please sign in to comment.