Skip to content

Commit 416ea49

Browse files
committed
perf: JsUtf16Buffer
1 parent 5b018e9 commit 416ea49

File tree

5 files changed

+228
-105
lines changed

5 files changed

+228
-105
lines changed

crates/node_binding/binding.d.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -737,15 +737,15 @@ export interface JsLoaderContext {
737737
_module: JsModule
738738
hot: Readonly<boolean>
739739
/** Content maybe empty in pitching stage */
740-
content: null | Buffer | string
740+
content: null | Buffer | JsUtf16Buffer
741741
additionalData?: any
742-
__internal__parseMeta: Record<string, string>
742+
__internal__parseMeta: Record<string, JsUtf16Buffer>
743743
sourceMap?: JsSourceMap
744744
cacheable: boolean
745-
fileDependencies: Array<string>
746-
contextDependencies: Array<string>
747-
missingDependencies: Array<string>
748-
buildDependencies: Array<string>
745+
fileDependencies: Array<JsUtf16Buffer>
746+
contextDependencies: Array<JsUtf16Buffer>
747+
missingDependencies: Array<JsUtf16Buffer>
748+
buildDependencies: Array<JsUtf16Buffer>
749749
loaderItems: Array<JsLoaderItem>
750750
loaderIndex: number
751751
loaderState: Readonly<JsLoaderState>
@@ -871,12 +871,12 @@ export interface JsRuntimeRequirementInTreeResult {
871871

872872
export interface JsSourceMap {
873873
version: number
874-
file?: string
875-
sources: Array<string>
876-
sourcesContent?: Array<string>
877-
names: Array<string>
878-
mappings: string
879-
sourceRoot?: string
874+
file?: JsUtf16Buffer
875+
sources: Array<JsUtf16Buffer>
876+
sourcesContent?: Array<JsUtf16Buffer>
877+
names: Array<JsUtf16Buffer>
878+
mappings: JsUtf16Buffer
879+
sourceRoot?: JsUtf16Buffer
880880
}
881881

882882
export interface JsStatsAsset {

crates/node_binding/src/plugins/js_loader/context.rs

Lines changed: 83 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,43 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, sync::Arc};
22

33
use napi::bindgen_prelude::*;
44
use napi_derive::napi;
55
use rspack_core::{rspack_sources::SourceMap, LoaderContext, RunnerContext};
66
use rspack_loader_runner::{LoaderItem, State as LoaderState};
7-
use rspack_napi::{
8-
napi::JsString, string::JsStringExt, threadsafe_js_value_ref::ThreadsafeJsValueRef,
9-
};
7+
use rspack_napi::{threadsafe_js_value_ref::ThreadsafeJsValueRef, JsUtf16Buffer};
108

119
use crate::{JsModuleWrapper, JsResourceData, JsRspackError};
1210

1311
#[napi(object)]
1412
pub struct JsSourceMap {
1513
pub version: u8,
16-
pub file: Option<JsString>,
17-
pub sources: Vec<JsString>,
18-
pub sources_content: Option<Vec<JsString>>,
19-
pub names: Vec<JsString>,
20-
pub mappings: JsString,
21-
pub source_root: Option<JsString>,
14+
pub file: Option<JsUtf16Buffer>,
15+
pub sources: Vec<JsUtf16Buffer>,
16+
pub sources_content: Option<Vec<JsUtf16Buffer>>,
17+
pub names: Vec<JsUtf16Buffer>,
18+
pub mappings: JsUtf16Buffer,
19+
pub source_root: Option<JsUtf16Buffer>,
2220
}
2321

24-
pub struct JsSourceMapWrapper(SourceMap);
22+
impl From<&SourceMap> for JsSourceMap {
23+
fn from(value: &SourceMap) -> Self {
24+
let file = value.file().map(Into::into);
2525

26-
impl JsSourceMapWrapper {
27-
pub fn new(source_map: SourceMap) -> Self {
28-
Self(source_map)
29-
}
26+
let sources = value.sources().iter().map(Into::into).collect::<Vec<_>>();
3027

31-
pub fn take(self) -> SourceMap {
32-
self.0
33-
}
34-
}
28+
let sources_content = value
29+
.sources_content()
30+
.iter()
31+
.map(Into::into)
32+
.collect::<Vec<_>>();
3533

36-
impl ToNapiValue for JsSourceMapWrapper {
37-
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
38-
let env_wrapper = Env::from_raw(env);
39-
40-
let file = match val.0.file() {
41-
Some(s) => Some(env_wrapper.create_string(s)?),
42-
None => None,
43-
};
44-
let mut sources = Vec::with_capacity(val.0.sources().len());
45-
for source in val.0.sources() {
46-
sources.push(env_wrapper.create_string(source)?);
47-
}
48-
let mut sources_content = Vec::with_capacity(val.0.sources_content().len());
49-
for source_content in val.0.sources_content() {
50-
sources_content.push(env_wrapper.create_string(source_content)?);
51-
}
52-
let mut names = Vec::with_capacity(val.0.sources_content().len());
53-
for name in val.0.names() {
54-
names.push(env_wrapper.create_string(name)?);
55-
}
56-
let mappings = env_wrapper.create_string(val.0.mappings())?;
57-
let source_root = match val.0.source_root() {
58-
Some(s) => Some(env_wrapper.create_string(s)?),
59-
None => None,
60-
};
34+
let names = value.names().iter().map(Into::into).collect::<Vec<_>>();
6135

62-
let js_source_map = JsSourceMap {
36+
let mappings = value.mappings().into();
37+
38+
let source_root = value.source_root().map(Into::into);
39+
40+
Self {
6341
version: 3,
6442
file,
6543
sources,
@@ -71,37 +49,55 @@ impl ToNapiValue for JsSourceMapWrapper {
7149
names,
7250
mappings,
7351
source_root,
74-
};
75-
ToNapiValue::to_napi_value(env, js_source_map)
52+
}
7653
}
7754
}
7855

79-
impl FromNapiValue for JsSourceMapWrapper {
80-
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
81-
let js_source_map: JsSourceMap = FromNapiValue::from_napi_value(env, napi_val)?;
82-
83-
let sources_content = match js_source_map.sources_content {
84-
Some(sources_content) => sources_content
85-
.into_iter()
86-
.map(|source| source.into_string())
87-
.collect::<Vec<_>>(),
88-
None => vec![],
89-
};
56+
impl From<&JsSourceMap> for SourceMap {
57+
fn from(value: &JsSourceMap) -> Self {
58+
let file = value.file.as_ref().map(|file| file.to_string());
9059

91-
Ok(JsSourceMapWrapper(SourceMap::new(
92-
js_source_map.mappings.into_string(),
93-
js_source_map
60+
let sources = Arc::from(
61+
value
9462
.sources
95-
.into_iter()
96-
.map(|source| source.into_string())
63+
.iter()
64+
.map(|source| source.to_string())
9765
.collect::<Vec<_>>(),
98-
sources_content,
99-
js_source_map
66+
);
67+
68+
let sources_content = Arc::from(
69+
value
70+
.sources_content
71+
.as_ref()
72+
.map(|sources_content| {
73+
sources_content
74+
.iter()
75+
.map(|source_content| source_content.to_string())
76+
.collect::<Vec<_>>()
77+
})
78+
.unwrap_or_default(),
79+
);
80+
81+
let names = Arc::from(
82+
value
10083
.names
101-
.into_iter()
102-
.map(|source| source.into_string())
84+
.iter()
85+
.map(|name| name.to_string())
10386
.collect::<Vec<_>>(),
104-
)))
87+
);
88+
89+
let mappings = Arc::from(value.mappings.to_string());
90+
91+
let source_root = value
92+
.source_root
93+
.as_ref()
94+
.map(|source_root| Arc::from(source_root.to_string()));
95+
96+
let mut source_map = Self::new(mappings, sources, sources_content, names);
97+
source_map.set_file(file);
98+
source_map.set_source_root(source_root);
99+
100+
source_map
105101
}
106102
}
107103

@@ -159,18 +155,18 @@ pub struct JsLoaderContext {
159155
pub hot: bool,
160156

161157
/// Content maybe empty in pitching stage
162-
pub content: Either3<Null, Buffer, String>,
158+
pub content: Either3<Null, Buffer, JsUtf16Buffer>,
163159
#[napi(ts_type = "any")]
164160
pub additional_data: Option<ThreadsafeJsValueRef<Unknown>>,
165161
#[napi(js_name = "__internal__parseMeta")]
166-
pub parse_meta: HashMap<String, String>,
162+
pub parse_meta: HashMap<String, JsUtf16Buffer>,
167163
#[napi(ts_type = "JsSourceMap")]
168-
pub source_map: Option<JsSourceMapWrapper>,
164+
pub source_map: Option<JsSourceMap>,
169165
pub cacheable: bool,
170-
pub file_dependencies: Vec<String>,
171-
pub context_dependencies: Vec<String>,
172-
pub missing_dependencies: Vec<String>,
173-
pub build_dependencies: Vec<String>,
166+
pub file_dependencies: Vec<JsUtf16Buffer>,
167+
pub context_dependencies: Vec<JsUtf16Buffer>,
168+
pub missing_dependencies: Vec<JsUtf16Buffer>,
169+
pub build_dependencies: Vec<JsUtf16Buffer>,
174170

175171
pub loader_items: Vec<JsLoaderItem>,
176172
pub loader_index: i32,
@@ -199,37 +195,42 @@ impl TryFrom<&mut LoaderContext<RunnerContext>> for JsLoaderContext {
199195
hot: cx.hot,
200196
content: match cx.content() {
201197
Some(c) => match c {
202-
rspack_core::Content::String(s) => Either3::C(s.to_string()),
198+
rspack_core::Content::String(s) => Either3::C(s.into()),
203199
rspack_core::Content::Buffer(vec) => Either3::B(vec.clone().into()),
204200
},
205201
None => Either3::A(Null),
206202
},
207-
parse_meta: cx.parse_meta.clone().into_iter().collect(),
203+
parse_meta: cx
204+
.parse_meta
205+
.clone()
206+
.into_iter()
207+
.map(|(key, value)| (key, (&value).into()))
208+
.collect(),
208209
additional_data: cx
209210
.additional_data()
210211
.and_then(|data| data.get::<ThreadsafeJsValueRef<Unknown>>())
211212
.cloned(),
212-
source_map: cx.source_map().cloned().map(JsSourceMapWrapper::new),
213+
source_map: cx.source_map().map(Into::into),
213214
cacheable: cx.cacheable,
214215
file_dependencies: cx
215216
.file_dependencies
216217
.iter()
217-
.map(|i| i.to_string_lossy().to_string())
218+
.map(|i| JsUtf16Buffer::from(i.to_string_lossy().as_ref()))
218219
.collect(),
219220
context_dependencies: cx
220221
.context_dependencies
221222
.iter()
222-
.map(|i| i.to_string_lossy().to_string())
223+
.map(|i| JsUtf16Buffer::from(i.to_string_lossy().as_ref()))
223224
.collect(),
224225
missing_dependencies: cx
225226
.missing_dependencies
226227
.iter()
227-
.map(|i| i.to_string_lossy().to_string())
228+
.map(|i| JsUtf16Buffer::from(i.to_string_lossy().as_ref()))
228229
.collect(),
229230
build_dependencies: cx
230231
.build_dependencies
231232
.iter()
232-
.map(|i| i.to_string_lossy().to_string())
233+
.map(|i| JsUtf16Buffer::from(i.to_string_lossy().as_ref()))
233234
.collect(),
234235

235236
loader_items: cx.loader_items.iter().map(Into::into).collect(),

crates/node_binding/src/plugins/js_loader/scheduler.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,59 @@ pub(crate) fn merge_loader_context(
5050
error.message,
5151
error.stack,
5252
error.hide_stack,
53-
from.file_dependencies,
54-
from.context_dependencies,
55-
from.missing_dependencies,
56-
from.build_dependencies,
53+
from
54+
.file_dependencies
55+
.into_iter()
56+
.map(|i| i.to_string())
57+
.collect::<Vec<_>>(),
58+
from
59+
.context_dependencies
60+
.into_iter()
61+
.map(|i| i.to_string())
62+
.collect::<Vec<_>>(),
63+
from
64+
.missing_dependencies
65+
.into_iter()
66+
.map(|i| i.to_string())
67+
.collect::<Vec<_>>(),
68+
from
69+
.build_dependencies
70+
.into_iter()
71+
.map(|i| i.to_string())
72+
.collect::<Vec<_>>(),
5773
)
5874
.into(),
5975
);
6076
}
6177

6278
to.cacheable = from.cacheable;
63-
to.file_dependencies = from.file_dependencies.into_iter().map(Into::into).collect();
79+
to.file_dependencies = from
80+
.file_dependencies
81+
.into_iter()
82+
.map(|i| Into::into(i.to_string()))
83+
.collect();
6484
to.context_dependencies = from
6585
.context_dependencies
6686
.into_iter()
67-
.map(Into::into)
87+
.map(|i| Into::into(i.to_string()))
6888
.collect();
6989
to.missing_dependencies = from
7090
.missing_dependencies
7191
.into_iter()
72-
.map(Into::into)
92+
.map(|i| Into::into(i.to_string()))
7393
.collect();
7494
to.build_dependencies = from
7595
.build_dependencies
7696
.into_iter()
77-
.map(Into::into)
97+
.map(|i| Into::into(i.to_string()))
7898
.collect();
7999

80100
let content = match from.content {
81101
Either3::A(_) => None,
82102
Either3::B(c) => Some(rspack_core::Content::from(Into::<Vec<u8>>::into(c))),
83-
Either3::C(s) => Some(rspack_core::Content::from(s)),
103+
Either3::C(s) => Some(rspack_core::Content::from(s.to_string())),
84104
};
85-
let source_map = from.source_map.map(|s| s.take());
105+
let source_map = from.source_map.as_ref().map(Into::into);
86106
let additional_data = from.additional_data.take().map(|data| {
87107
let mut additional = AdditionalData::default();
88108
additional.insert(data);
@@ -109,7 +129,11 @@ pub(crate) fn merge_loader_context(
109129
})
110130
.collect();
111131
to.loader_index = from.loader_index;
112-
to.parse_meta = from.parse_meta.into_iter().collect();
132+
to.parse_meta = from
133+
.parse_meta
134+
.into_iter()
135+
.map(|(key, value)| (key, value.to_string()))
136+
.collect();
113137

114138
Ok(())
115139
}

0 commit comments

Comments
 (0)