Closed
Description
If you try to find a specific character in a string, it performs significantly worse than the theoretical optimal implementation built on memchr
.
For example, consider the following benchmark. On my laptop the find
method runs at about 90ns/iter, and the memchr
method runs at 4ns/iter. I would imagine that we should optimize them such that they are the same.
#![feature(test)]
extern crate memchr;
extern crate test;
use test::{black_box, Bencher};
const DEMO_STRING: &str = "this is a string with a decent number of ascii characters and \n there is a new line in the middle which it should find";
#[bench]
fn bench_find(b: &mut Bencher) {
b.iter(|| {
let s = test::black_box(DEMO_STRING);
s.find('\n')
});
}
#[bench]
fn bench_memchr(b: &mut Bencher) {
b.iter(|| {
let s = test::black_box(DEMO_STRING);
memchr::memchr(b'\n', s.as_bytes())
});
}