chore: add rayon parallel dependency and release profile optimizations to jest_rust
Add rayon 1.10.0 as a dependency in Cargo.toml and refactor Stats processing into a dedicated method that uses `par_iter()` for parallel file processing. Configure release profile with thin LTO, disabled debug symbols, and abort-on-panic for maximum runtime performance. Update README with build and benchmark integration instructions for the jest_rust project.
This commit is contained in:
+54
-43
@@ -1,3 +1,4 @@
|
||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||
use std::{env, fmt::Display, fs};
|
||||
|
||||
static FORBIDDEN_WORDS: &'static [&'static str] = &[
|
||||
@@ -39,7 +40,7 @@ static FORBIDDEN_WORDS: &'static [&'static str] = &[
|
||||
];
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Stats {
|
||||
pub struct Stats {
|
||||
file_count: u32,
|
||||
failed_file_count: u32,
|
||||
|
||||
@@ -50,6 +51,48 @@ struct Stats {
|
||||
numeric_count: u32,
|
||||
forbidden_count: u32,
|
||||
}
|
||||
impl Stats {
|
||||
pub fn process(&mut self, file: &str) {
|
||||
let Ok(text) = fs::read_to_string(&file) else {
|
||||
self.failed_file_count += 1;
|
||||
return;
|
||||
};
|
||||
self.file_count += 1;
|
||||
for sentence in text
|
||||
.split('.')
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| !s.is_empty())
|
||||
{
|
||||
self.sentence_count += 1;
|
||||
for word in sentence
|
||||
.split_whitespace()
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| !s.is_empty())
|
||||
{
|
||||
self.word_count += 1;
|
||||
//get all numbers counted
|
||||
let mut all_capitalized = true;
|
||||
for char in word.chars() {
|
||||
if char.is_numeric() {
|
||||
self.numeric_count += 1;
|
||||
}
|
||||
if !char.is_ascii_uppercase() {
|
||||
all_capitalized = false;
|
||||
}
|
||||
}
|
||||
if all_capitalized {
|
||||
self.capitalized_count += 1;
|
||||
}
|
||||
let lowercase_word = word.to_lowercase();
|
||||
for forbidden_word in FORBIDDEN_WORDS {
|
||||
if lowercase_word.contains(forbidden_word) {
|
||||
self.forbidden_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Display for Stats {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
writeln!(f, "file count: {}", self.file_count)?;
|
||||
@@ -83,48 +126,16 @@ impl Display for Stats {
|
||||
|
||||
fn main() {
|
||||
let files = env::args().skip(1);
|
||||
let mut stats = Stats::default();
|
||||
for file in files {
|
||||
let Ok(text) = fs::read_to_string(&file) else {
|
||||
stats.failed_file_count += 1;
|
||||
continue;
|
||||
};
|
||||
stats.file_count += 1;
|
||||
for sentence in text
|
||||
.split('.')
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| !s.is_empty())
|
||||
{
|
||||
stats.sentence_count += 1;
|
||||
for word in sentence
|
||||
.split_whitespace()
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| !s.is_empty())
|
||||
{
|
||||
stats.word_count += 1;
|
||||
//get all numbers counted
|
||||
let mut all_capitalized = true;
|
||||
for char in word.chars() {
|
||||
if char.is_numeric() {
|
||||
stats.numeric_count += 1;
|
||||
}
|
||||
if !char.is_ascii_uppercase() {
|
||||
all_capitalized = false;
|
||||
}
|
||||
}
|
||||
if all_capitalized {
|
||||
stats.capitalized_count += 1;
|
||||
}
|
||||
let lowercase_word = word.to_lowercase();
|
||||
for forbidden_word in FORBIDDEN_WORDS {
|
||||
if lowercase_word.contains(forbidden_word) {
|
||||
stats.forbidden_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{stats}");
|
||||
// let mut stats = Stats::default();
|
||||
// for file in files {
|
||||
// stats.process(&file);
|
||||
// }
|
||||
let files = files.collect::<Vec<_>>();
|
||||
files.par_iter().for_each(|file| {
|
||||
let mut stats = Stats::default();
|
||||
stats.process(&file);
|
||||
println!("{stats}");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user