forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#67249 - ranma42:improve-starts-with-literal…
…-char, r=BurntSushi Improve code generated for `starts_with(<literal char>)` This PR includes two minor improvements to the code generated when checking for string prefix/suffix. The first commit simplifies the str/str operation, by taking advantage of the raw UTF-8 representation. The second commit replaces the current str/char matching logic with a char->str encoding and then the previous method. The resulting code should be equivalent in the generic case (one char is being encoded versus one char being decoded), but it becomes easy to optimize in the case of a literal char, which in most cases a developer might expect to be at least as simple as that of a literal string. This PR should fix rust-lang#41993
- Loading branch information
Showing
3 changed files
with
48 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -11,4 +11,5 @@ mod hash; | |
mod iter; | ||
mod num; | ||
mod ops; | ||
mod pattern; | ||
mod slice; |
This file contains 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,43 @@ | ||
use test::black_box; | ||
use test::Bencher; | ||
|
||
#[bench] | ||
fn starts_with_char(b: &mut Bencher) { | ||
let text = black_box("kdjsfhlakfhlsghlkvcnljknfqiunvcijqenwodind"); | ||
b.iter(|| { | ||
for _ in 0..1024 { | ||
black_box(text.starts_with('k')); | ||
} | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn starts_with_str(b: &mut Bencher) { | ||
let text = black_box("kdjsfhlakfhlsghlkvcnljknfqiunvcijqenwodind"); | ||
b.iter(|| { | ||
for _ in 0..1024 { | ||
black_box(text.starts_with("k")); | ||
} | ||
}) | ||
} | ||
|
||
|
||
#[bench] | ||
fn ends_with_char(b: &mut Bencher) { | ||
let text = black_box("kdjsfhlakfhlsghlkvcnljknfqiunvcijqenwodind"); | ||
b.iter(|| { | ||
for _ in 0..1024 { | ||
black_box(text.ends_with('k')); | ||
} | ||
}) | ||
} | ||
|
||
#[bench] | ||
fn ends_with_str(b: &mut Bencher) { | ||
let text = black_box("kdjsfhlakfhlsghlkvcnljknfqiunvcijqenwodind"); | ||
b.iter(|| { | ||
for _ in 0..1024 { | ||
black_box(text.ends_with("k")); | ||
} | ||
}) | ||
} |
This file contains 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