Port perf_convert_ascii

The "#[bench]" attribute is not allowed in stable Rust, so keep it behind
a new feature flag. Run on nightly Rust with

    $ cargo bench --features=bechmark
    test tests::encoding::bench::bench_convert_ascii ... bench:     125,988 ns/iter (+/- 1,128) = 1040 MB/s
This commit is contained in:
Johannes Altmanninger 2023-12-10 08:48:18 +01:00
parent f3dd8d306f
commit 38d52b7835
5 changed files with 17 additions and 19 deletions

View File

@ -78,6 +78,7 @@ path = "fish-rust/src/lib.rs"
# These tests are run by fish_tests().
default = ["fish-ffi-tests"]
fish-ffi-tests = ["inventory"]
benchmark = []
# The following features are auto-detected by the build-script and should not be enabled manually.
asan = []

View File

@ -1,3 +1,4 @@
#![cfg_attr(feature = "benchmark", feature(test))]
#![allow(non_camel_case_types)]
#![allow(dead_code)]
#![allow(non_upper_case_globals)]

View File

@ -17,3 +17,17 @@ fn test_convert_nulls() {
let out_wstr = str2wcstring(&out_str);
assert_eq!(input, &out_wstr);
}
#[cfg(feature = "benchmark")]
mod bench {
extern crate test;
use crate::tests::encoding::str2wcstring;
use test::Bencher;
#[bench]
fn bench_convert_ascii(b: &mut Bencher) {
let s: [u8; 128 * 1024] = std::array::from_fn(|i| b'0' + u8::try_from(i % 10).unwrap());
b.bytes = u64::try_from(s.len()).unwrap();
b.iter(|| str2wcstring(&s));
}
}

View File

@ -6,6 +6,7 @@ mod complete;
mod debounce;
#[cfg(test)]
mod editable_line;
#[cfg(test)]
mod encoding;
mod env;
mod env_universal_common;

View File

@ -408,24 +408,6 @@ static void test_convert_private_use() {
}
}
// todo!("port this");
static void perf_convert_ascii() {
std::string s(128 * 1024, '\0');
for (size_t i = 0; i < s.size(); i++) {
s[i] = (i % 10) + '0';
}
(void)str2wcstring(s);
double start = timef();
const int iters = 1024;
for (int i = 0; i < iters; i++) {
(void)str2wcstring(s);
}
double end = timef();
auto usec = static_cast<unsigned long long>(((end - start) * 1E6) / iters);
say(L"ASCII string conversion perf: %lu bytes in %llu usec", s.size(), usec);
}
// todo!("already ported, delete this");
static void test_iothread() {
say(L"Testing iothreads");
@ -1203,7 +1185,6 @@ static const test_t s_tests[]{
{TEST_GROUP("convert"), test_convert},
{TEST_GROUP("convert"), test_convert_private_use},
{TEST_GROUP("convert_ascii"), test_convert_ascii},
{TEST_GROUP("perf_convert_ascii"), perf_convert_ascii, true},
{TEST_GROUP("iothread"), test_iothread},
{TEST_GROUP("pthread"), test_pthread},
{TEST_GROUP("lru"), test_lru},