Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use simdutf8 to validate UTF-8 when reading files #5196

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ seq-macro = "0.3.5"
serde = "1.0.206"
serde_json = "1.0.124"
serde-wasm-bindgen = "0.6.5"
simdutf8 = "0.1.4"
shulaoda marked this conversation as resolved.
Show resolved Hide resolved
similar = "2.6.0"
syn = { version = "2.0.74", default-features = false }
tempfile = "3.12.0"
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ once_cell = { workspace = true }
memchr = { workspace = true }
json-strip-comments = { workspace = true }
schemars = { workspace = true, features = ["indexmap2"] }
simdutf8 = { workspace = true }

[dev-dependencies]
insta = { workspace = true }
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_linter/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub use self::{
settings::{jsdoc::JSDocPluginSettings, OxlintSettings},
};
use crate::{
rules::RuleEnum, utils::is_jest_rule_adapted_to_vitest, AllowWarnDeny, RuleWithSeverity,
rules::RuleEnum,
utils::{is_jest_rule_adapted_to_vitest, read_to_string},
AllowWarnDeny, RuleWithSeverity,
};

/// Oxlint Configuration File
Expand Down Expand Up @@ -68,7 +70,7 @@ impl OxlintConfig {
///
/// * Parse Failure
pub fn from_file(path: &Path) -> Result<Self, OxcDiagnostic> {
let mut string = std::fs::read_to_string(path).map_err(|e| {
let mut string = read_to_string(path).map_err(|e| {
OxcDiagnostic::error(format!("Failed to parse config {path:?} with error {e:?}"))
})?;

Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rustc_hash::FxHashSet;

use crate::{
partial_loader::{JavaScriptSource, PartialLoader, LINT_PARTIAL_LOADER_EXT},
utils::read_to_string,
Fixer, Linter, Message,
};

Expand Down Expand Up @@ -176,7 +177,7 @@ impl Runtime {
return None;
}
let source_type = source_type.unwrap_or_default();
let file_result = fs::read_to_string(path).map_err(|e| {
let file_result = read_to_string(path).map_err(|e| {
Error::new(OxcDiagnostic::error(format!(
"Failed to open file {path:?} with error \"{e}\""
)))
Expand Down
16 changes: 16 additions & 0 deletions crates/oxc_linter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ mod tree_shaking;
mod unicorn;
mod vitest;

use std::{io, path::Path};

pub use self::{
config::*, jest::*, jsdoc::*, nextjs::*, promise::*, react::*, react_perf::*, tree_shaking::*,
unicorn::*, vitest::*,
Expand Down Expand Up @@ -37,3 +39,17 @@ pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool {

jest_rules.contains(&rule_name)
}

pub fn read_to_string(path: &Path) -> io::Result<String> {
// `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally
let bytes = std::fs::read(path)?;
if simdutf8::basic::from_utf8(&bytes).is_err() {
// Same error as `fs::read_to_string` produces (`io::Error::INVALID_UTF8`)
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"stream did not contain valid UTF-8",
));
}
// SAFETY: `simdutf8` has ensured it's a valid UTF-8 string
Ok(unsafe { String::from_utf8_unchecked(bytes) })
}
Loading