-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
tr: add codspeed benchmark #12189
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
Open
parasol-aser
wants to merge
2
commits into
uutils:main
Choose a base branch
from
parasol-aser:tr/codspeed-bench
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−0
Open
tr: add codspeed benchmark #12189
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ jobs: | |
| uu_split, | ||
| uu_tee, | ||
| uu_timeout, | ||
| uu_tr, | ||
| uu_tsort, | ||
| uu_unexpand, | ||
| uu_uniq, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // This file is part of the uutils coreutils package. | ||
| // | ||
| // For the full copyright and license information, please view the LICENSE | ||
| // file that was distributed with this source code. | ||
|
|
||
| // spell-checker:ignore aeiou | ||
|
|
||
| //! Benchmarks for `tr`. | ||
| //! | ||
| //! `tr` only reads stdin, so each bench redirects fd 0 onto a prepared | ||
| //! input file before invoking `uumain`. fd 1 is redirected to /dev/null | ||
| //! so the translated output does not flood the harness's terminal. | ||
| //! Both fds are restored after each iteration. | ||
|
|
||
| #[cfg(unix)] | ||
| mod benches { | ||
| use divan::{Bencher, black_box}; | ||
| use uu_tr::uumain; | ||
| use uucore::benchmark::{run_util_function, setup_test_file, text_data}; | ||
|
|
||
| fn bench_tr_with_stdin(bencher: Bencher, data: &[u8], args: &[&str]) { | ||
| let file_path = setup_test_file(data); | ||
| let file = std::fs::File::open(file_path).unwrap(); | ||
| let devnull = std::fs::OpenOptions::new() | ||
| .write(true) | ||
| .open("/dev/null") | ||
| .unwrap(); | ||
| let stdin_bak = rustix::io::dup(rustix::stdio::stdin()).unwrap(); | ||
| let stdout_bak = rustix::io::dup(rustix::stdio::stdout()).unwrap(); | ||
|
|
||
| bencher.bench_local(|| { | ||
| use rustix::stdio::{dup2_stdin, dup2_stdout}; | ||
| rustix::fs::seek(&file, rustix::fs::SeekFrom::Start(0)).unwrap(); | ||
| dup2_stdin(&file).unwrap(); | ||
| dup2_stdout(&devnull).unwrap(); | ||
| black_box(run_util_function(uumain, args)); | ||
| dup2_stdin(&stdin_bak).unwrap(); | ||
| dup2_stdout(&stdout_bak).unwrap(); | ||
| }); | ||
| } | ||
|
|
||
| const SIZE_MB: usize = 16; | ||
|
|
||
| /// ASCII lowercase->uppercase range translation. | ||
| /// Exercises the AVX2 ASCII-range fast path on x86_64 hosts that | ||
| /// support it, and the scalar range fallback on other targets. | ||
| #[divan::bench] | ||
| fn tr_ascii_range_lower_to_upper(bencher: Bencher) { | ||
| let data = text_data::generate_by_size(SIZE_MB, 80); | ||
| bench_tr_with_stdin(bencher, &data, &["a-z", "A-Z"]); | ||
| } | ||
|
|
||
| /// Single-character replacement. Exercises the existing | ||
| /// `process_single_char_replace` SIMD path; guards against | ||
| /// regressions outside the new range fast path. | ||
| #[divan::bench] | ||
| fn tr_single_char_replace(bencher: Bencher) { | ||
| let data = text_data::generate_by_size(SIZE_MB, 80); | ||
| bench_tr_with_stdin(bencher, &data, &["a", "b"]); | ||
| } | ||
|
|
||
| /// Multi-character set translation. Falls through to the | ||
| /// 256-byte translation table path (no fast path applies). | ||
| #[divan::bench] | ||
| fn tr_multi_char_translate(bencher: Bencher) { | ||
| let data = text_data::generate_by_size(SIZE_MB, 80); | ||
| bench_tr_with_stdin(bencher, &data, &["aeiou", "AEIOU"]); | ||
| } | ||
|
|
||
| /// Delete an ASCII range — covers the deletion path. | ||
| #[divan::bench] | ||
| fn tr_delete_ascii_range(bencher: Bencher) { | ||
| let data = text_data::generate_by_size(SIZE_MB, 80); | ||
| bench_tr_with_stdin(bencher, &data, &["-d", "a-z"]); | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| divan::main(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why all these unix cfg ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I kept the benchmark Unix-gated because it redirects stdin/stdout with rustix fd helpers and /dev/null, but grouped the benchmark code under one cfg(unix) module to make that clearer.