Skip to content

Commit

Permalink
chore: try put parse in rayon thread
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Sep 22, 2024
1 parent 45a3997 commit 696f506
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions crates/rspack_core/src/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use rspack_util::{
};
use rustc_hash::FxHasher;
use serde_json::json;
use tokio::sync::oneshot;

use crate::{
add_connection_states, contextify, diagnostics::ModuleBuildError, get_context,
Expand Down Expand Up @@ -485,7 +486,33 @@ impl Module for NormalModule {
build_info.context_dependencies = loader_result.context_dependencies;
build_info.missing_dependencies = loader_result.missing_dependencies;
build_info.build_dependencies = loader_result.build_dependencies;

let (send, receive) = oneshot::channel();
rayon::scope(|s| {
s.spawn(|_| {
let x = self.parser_and_generator.parse(ParseContext {
source: original_source.clone(),
module_context: &self.context,
module_identifier: self.identifier(),
module_parser_options: self.parser_options.as_ref(),
module_type: &self.module_type,
module_layer: self.layer.as_ref(),
module_user_request: &self.user_request,
module_source_map_kind: *self.get_source_map_kind(),
loaders: &self.loaders,
resource_data: &self.resource_data,
compiler_options: build_context.compiler_options,
additional_data: loader_result.additional_data,
build_info: &mut build_info,
build_meta: &mut build_meta,
parse_meta: loader_result.parse_meta,
});
let _ = send.send(x);
});
});
let result = receive
.await
.expect("receive msg failed")?
.split_into_parts();
let (
ParseResult {
source,
Expand All @@ -496,26 +523,7 @@ impl Module for NormalModule {
side_effects_bailout,
},
diagnostics,
) = self
.parser_and_generator
.parse(ParseContext {
source: original_source.clone(),
module_context: &self.context,
module_identifier: self.identifier(),
module_parser_options: self.parser_options.as_ref(),
module_type: &self.module_type,
module_layer: self.layer.as_ref(),
module_user_request: &self.user_request,
module_source_map_kind: *self.get_source_map_kind(),
loaders: &self.loaders,
resource_data: &self.resource_data,
compiler_options: build_context.compiler_options,
additional_data: loader_result.additional_data,
build_info: &mut build_info,
build_meta: &mut build_meta,
parse_meta: loader_result.parse_meta,
})?
.split_into_parts();
) = result;
if !diagnostics.is_empty() {
self.add_diagnostics(diagnostics);
build_meta = self.last_successful_build_meta.clone();
Expand Down

0 comments on commit 696f506

Please sign in to comment.