Skip to content

Commit b8f4b11

Browse files
authored
perf: use simd json for json parse (#117)
1 parent 0c268e1 commit b8f4b11

File tree

12 files changed

+384
-134
lines changed

12 files changed

+384
-134
lines changed

Cargo.lock

Lines changed: 100 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ serde = { version = "1.0.203", features = ["derive"] } # derive for Deserialize
8181
serde_json = { version = "1.0.117", features = [
8282
"preserve_order",
8383
] } # preserve_order: package_json.exports requires order such as `["require", "import", "default"]`
84+
85+
simd-json = { version = "0.17.0", features = ["serde_impl", "runtime-detection"], default-features = false }
86+
8487
rustc-hash = { version = "2.0.0", default-features = false, features = ["std"] }
8588
dunce = "1.0.4" # Normalize Windows paths to the most compatible format, avoiding UNC where possible
8689
thiserror = "1.0.61"

deny.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ allow = [
9393
"Apache-2.0",
9494
"Unicode-DFS-2016",
9595
"BSD-2-Clause",
96-
"Unicode-3.0"
96+
"Unicode-3.0",
97+
"Zlib"
9798
#"Apache-2.0 WITH LLVM-exception",
9899
]
99100
# The confidence threshold for detecting a license from license text.

napi/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ async fn resolve(resolver: &Resolver, path: &Path, request: &str) -> ResolveResu
3030
error: None,
3131
module_type: resolution
3232
.package_json()
33-
.and_then(|p| p.r#type.as_ref())
34-
.and_then(|t| t.as_str())
33+
.and_then(|p| p.r#type)
3534
.map(|t| t.to_string()),
3635
},
3736
Err(err) => ResolveResult {

src/cache.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ use rustc_hash::FxHasher;
1515
use tokio::sync::OnceCell as OnceLock;
1616

1717
use crate::{
18-
context::ResolveContext as Ctx, package_json::PackageJson, path::PathUtil, FileMetadata,
19-
FileSystem, ResolveError, ResolveOptions, TsConfig,
18+
context::ResolveContext as Ctx,
19+
package_json::{off_to_location, PackageJson},
20+
path::PathUtil,
21+
FileMetadata, FileSystem, JSONError, ResolveError, ResolveOptions, TsConfig,
2022
};
2123

2224
#[derive(Default)]
@@ -311,27 +313,40 @@ impl CachedPathImpl {
311313
.package_json
312314
.get_or_try_init(|| async {
313315
let package_json_path = self.path.join("package.json");
314-
let Ok(package_json_string) = fs.read_to_string(&package_json_path).await else {
316+
let Ok(package_json_string) = fs.read(&package_json_path).await else {
315317
return Ok(None);
316318
};
317319
let real_path = if options.symlinks {
318320
self.realpath(fs).await?.join("package.json")
319321
} else {
320322
package_json_path.clone()
321323
};
322-
PackageJson::parse(package_json_path.clone(), real_path, &package_json_string)
323-
.map(Arc::new)
324-
.map(Some)
325-
.map_err(|error| {
326-
ResolveError::from_serde_json_error(
327-
package_json_path,
328-
&error,
329-
Some(package_json_string),
330-
)
331-
})
324+
match PackageJson::parse(package_json_path.clone(), real_path, package_json_string) {
325+
Ok(v) => Ok(Some(Arc::new(v))),
326+
Err(parse_err) => {
327+
let package_json_path = self.path.join("package.json");
328+
let package_json_string = match fs.read_to_string(&package_json_path).await {
329+
Ok(c) => c,
330+
Err(io_err) => {
331+
return Err(ResolveError::from(io_err));
332+
}
333+
};
334+
335+
let (line, column) = off_to_location(&package_json_string, parse_err.index());
336+
337+
Err(ResolveError::JSON(JSONError {
338+
path: package_json_path,
339+
message: format!("Parsing error: {:?}", parse_err.error()),
340+
line,
341+
column,
342+
content: Some(package_json_string),
343+
}))
344+
}
345+
}
332346
})
333347
.await
334348
.cloned();
349+
335350
// https://github.com/webpack/enhanced-resolve/blob/58464fc7cb56673c9aa849e68e6300239601e615/lib/DescriptionFileUtils.js#L68-L82
336351
match &result {
337352
Ok(Some(package_json)) => {

0 commit comments

Comments
 (0)