about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThe8472 <git@infinite-source.de>2021-09-10 21:37:42 +0200
committerThe8472 <git@infinite-source.de>2021-09-11 00:25:41 +0200
commitb6278664af23004be3fbf9ecd64505e9abdf3644 (patch)
tree15aab672681faa15f75ec87d9dcc2a12cdcaab3f
parent4c44f061d80390a07081c7cefa5da3ddae7807c3 (diff)
downloadrust-b6278664af23004be3fbf9ecd64505e9abdf3644.tar.gz
rust-b6278664af23004be3fbf9ecd64505e9abdf3644.zip
optimize utf8_is_cont_byte() to speed up str.chars().count()
it shows consistent improvements across several x86_64 feature levels

```
old, -O2, x86-64
test str::str_char_count_emoji                                  ... bench:       1,924 ns/iter (+/- 26)
test str::str_char_count_lorem                                  ... bench:         879 ns/iter (+/- 12)
test str::str_char_count_lorem_short                            ... bench:           5 ns/iter (+/- 0)

new, -O2, x86-64
test str::str_char_count_emoji                                  ... bench:       1,878 ns/iter (+/- 21)
test str::str_char_count_lorem                                  ... bench:         851 ns/iter (+/- 11)
test str::str_char_count_lorem_short                            ... bench:           4 ns/iter (+/- 0)

old, -O2, x86-64-v2
test str::str_char_count_emoji                                  ... bench:       1,477 ns/iter (+/- 46)
test str::str_char_count_lorem                                  ... bench:         675 ns/iter (+/- 15)
test str::str_char_count_lorem_short                            ... bench:           5 ns/iter (+/- 0)

new, -O2, x86-64-v2
test str::str_char_count_emoji                                  ... bench:       1,323 ns/iter (+/- 39)
test str::str_char_count_lorem                                  ... bench:         593 ns/iter (+/- 18)
test str::str_char_count_lorem_short                            ... bench:           4 ns/iter (+/- 0)

old, -O2, x86-64-v3
test str::str_char_count_emoji                                  ... bench:         748 ns/iter (+/- 7)
test str::str_char_count_lorem                                  ... bench:         348 ns/iter (+/- 2)
test str::str_char_count_lorem_short                            ... bench:           5 ns/iter (+/- 0)

new, -O2, x86-64-v3
test str::str_char_count_emoji                                  ... bench:         650 ns/iter (+/- 4)
test str::str_char_count_lorem                                  ... bench:         301 ns/iter (+/- 1)
test str::str_char_count_lorem_short                            ... bench:           5 ns/iter (+/- 0)
```
-rw-r--r--library/core/src/str/validations.rs2
1 files changed, 1 insertions, 1 deletions
diff --git a/library/core/src/str/validations.rs b/library/core/src/str/validations.rs
index 373a8212425..fc8f47dced4 100644
--- a/library/core/src/str/validations.rs
+++ b/library/core/src/str/validations.rs
@@ -22,7 +22,7 @@ fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 {
 /// bits `10`).
 #[inline]
 pub(super) fn utf8_is_cont_byte(byte: u8) -> bool {
-    (byte & !CONT_MASK) == TAG_CONT_U8
+    (byte as i8) < -64
 }
 
 #[inline]