Skip to content

Commit 9684324

Browse files
Merge PR #194 feat/introducing-benchmarks
feat(Benchmarks): introduce benchmarks
2 parents 7695949 + 13ba0f5 commit 9684324

4 files changed

Lines changed: 153 additions & 6 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ rusty-hook = "0.11.2"
6060
fake = "4.3.0"
6161
uuid = { version = "1.16.0", features = ["v4", "fast-rng"] }
6262
http = "1.3"
63+
criterion = { version = "0.5", features = ["html_reports"] }
6364

65+
[[bench]]
66+
name = "utils"
67+
harness = false
6468

6569
[target.'cfg(windows)'.dependencies]
6670
windows-sys = { version = "0.59.0", features = ["Win32_Foundation", "Win32_System_Console", "Win32_UI_HiDpi"]}
6771

6872
[profile.release]
6973
codegen-units = 1
7074
lto = "fat"
75+
opt-level = "z"
76+
strip = "debuginfo"

benches/utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
2+
use manga_tui::SanitizedFilename;
3+
4+
fn sanitize_filename(c: &mut Criterion) {
5+
let example = "some / title :";
6+
7+
c.bench_with_input(BenchmarkId::new("sanitized string parsing", example), &example, |b, &ex| {
8+
b.iter(|| SanitizedFilename::new(ex));
9+
});
10+
}
11+
12+
criterion_group!(benches, sanitize_filename);
13+
criterion_main!(benches);

src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ impl SearchTerm {
4040
}
4141

4242
/// Remove special characteres that may cause errors when creating directories or files
43-
fn remove_conflicting_characteres<T: AsRef<Path>>(title: T) -> PathBuf {
43+
fn remove_conflicting_characteres<T: AsRef<str>>(title: T) -> PathBuf {
4444
let invalid_chars = ['\\', '/', ':', '*', '?', '"', '<', '>', '|'];
4545

46-
let title: &Path = title.as_ref();
47-
let title = title.to_str().unwrap().trim();
46+
let title: &str = title.as_ref().trim();
4847

4948
let sanitized_title: String = title.chars().map(|c| if invalid_chars.contains(&c) { '_' } else { c }).collect();
5049

@@ -63,7 +62,7 @@ impl Display for SanitizedFilename {
6362
}
6463

6564
impl SanitizedFilename {
66-
pub fn new<T: AsRef<Path>>(name: T) -> Self {
65+
pub fn new<T: AsRef<str>>(name: T) -> Self {
6766
Self(remove_conflicting_characteres(name))
6867
}
6968

@@ -72,7 +71,7 @@ impl SanitizedFilename {
7271
}
7372
}
7473

75-
impl<T: AsRef<Path>> From<T> for SanitizedFilename {
74+
impl<T: AsRef<str>> From<T> for SanitizedFilename {
7675
fn from(value: T) -> Self {
7776
Self::new(value)
7877
}
@@ -161,7 +160,7 @@ mod test {
161160

162161
assert_eq!(Path::new("some name which contains _"), file_name.as_path());
163162

164-
let file_name = SanitizedFilename::new("some / name / which contains ");
163+
let file_name = SanitizedFilename::new(" some / name / which contains ");
165164

166165
assert_eq!(Path::new("some _ name _ which contains"), file_name.as_path())
167166
}

0 commit comments

Comments
 (0)