Skip to content

Commit

Permalink
perf: cache SWC loader (#7965)
Browse files Browse the repository at this point in the history
* cache swc loader

* fix ident

* fix clippy
  • Loading branch information
CPunisher authored Oct 18, 2024
1 parent dc05694 commit 27f197c
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions crates/rspack_binding_options/src/plugins/js_loader/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::Arc;
use std::{
borrow::Cow,
sync::{Arc, LazyLock},
};

use rspack_collections::{Identifiable, Identifier};
use rspack_core::{
Expand All @@ -14,8 +17,10 @@ use rspack_hook::plugin_hook;
use rspack_loader_lightningcss::{config::Config, LIGHTNINGCSS_LOADER_IDENTIFIER};
use rspack_loader_preact_refresh::PREACT_REFRESH_LOADER_IDENTIFIER;
use rspack_loader_react_refresh::REACT_REFRESH_LOADER_IDENTIFIER;
use rspack_loader_swc::SWC_LOADER_IDENTIFIER;
use rspack_loader_swc::{SwcLoader, SWC_LOADER_IDENTIFIER};
use rspack_paths::Utf8Path;
use rustc_hash::FxHashMap;
use tokio::sync::RwLock;

use super::{JsLoaderRspackPlugin, JsLoaderRspackPluginInner};

Expand All @@ -39,15 +44,37 @@ pub fn serde_error_to_miette(
let span = LabeledSpan::at_offset(offset.offset(), e.to_string());
miette!(labels = vec![span], "{msg}").with_source_code(content.clone())
}
pub fn get_builtin_loader(builtin: &str, options: Option<&str>) -> Result<BoxLoader> {

type SwcLoaderCache<'a> = LazyLock<RwLock<FxHashMap<(Cow<'a, str>, Arc<str>), Arc<SwcLoader>>>>;
static SWC_LOADER_CACHE: SwcLoaderCache = LazyLock::new(|| RwLock::new(FxHashMap::default()));

pub async fn get_builtin_loader(builtin: &str, options: Option<&str>) -> Result<BoxLoader> {
let options: Arc<str> = options.unwrap_or("{}").into();
if builtin.starts_with(SWC_LOADER_IDENTIFIER) {
return Ok(Arc::new(
if let Some(loader) = SWC_LOADER_CACHE
.read()
.await
.get(&(Cow::Borrowed(builtin), options.clone()))
{
return Ok(loader.clone());
}

let loader = Arc::new(
rspack_loader_swc::SwcLoader::new(serde_json::from_str(options.as_ref()).map_err(|e| {
serde_error_to_miette(e, options, "failed to parse builtin:swc-loader options")
serde_error_to_miette(
e,
options.clone(),
"failed to parse builtin:swc-loader options",
)
})?)
.with_identifier(builtin.into()),
));
);

SWC_LOADER_CACHE.write().await.insert(
(Cow::Owned(builtin.to_owned()), options.clone()),
loader.clone(),
);
return Ok(loader);
}

if builtin.starts_with(LIGHTNINGCSS_LOADER_IDENTIFIER) {
Expand Down Expand Up @@ -115,7 +142,9 @@ pub(crate) async fn resolve_loader(

// FIXME: not belong to napi
if loader_request.starts_with(BUILTIN_LOADER_PREFIX) {
return get_builtin_loader(loader_request, loader_options).map(Some);
return get_builtin_loader(loader_request, loader_options)
.await
.map(Some);
}

let resolve_result = resolver
Expand Down

2 comments on commit 27f197c

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-10-18 d01e85c) Current Change
10000_development-mode + exec 2.14 s ± 16 ms 2.1 s ± 32 ms -1.92 %
10000_development-mode_hmr + exec 673 ms ± 11 ms 669 ms ± 9.2 ms -0.49 %
10000_production-mode + exec 2.68 s ± 42 ms 2.64 s ± 25 ms -1.48 %
arco-pro_development-mode + exec 1.79 s ± 90 ms 1.79 s ± 62 ms -0.18 %
arco-pro_development-mode_hmr + exec 427 ms ± 1.4 ms 427 ms ± 1.8 ms -0.07 %
arco-pro_production-mode + exec 3.18 s ± 64 ms 3.19 s ± 81 ms +0.39 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.17 s ± 81 ms 3.22 s ± 87 ms +1.72 %
threejs_development-mode_10x + exec 1.61 s ± 18 ms 1.61 s ± 15 ms -0.22 %
threejs_development-mode_10x_hmr + exec 756 ms ± 12 ms 761 ms ± 13 ms +0.75 %
threejs_production-mode_10x + exec 4.94 s ± 35 ms 4.98 s ± 23 ms +0.83 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rspress ✅ success
rslib ❌ failure
rsbuild ✅ success
examples ✅ success
devserver ✅ success

Please sign in to comment.