Skip to content

str::find(char) is slower than it ought ot be #46693

Closed
@mystor

Description

@mystor

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())
    });
}

Metadata

Metadata

Assignees

Labels

C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions