rust-lang/rust — pull request #15638 by blake2-ppc (2014-07-13 00:09:14+00:00)
Title: str: Implement a faster Chars iterator for &str

Reap the benefits of the valid UTF-8 representation -- implement
a faster UTF-8 decoding in the Chars iterator assuming valid data. To do
this we use an iterator similar to the slice iterator.

The implementation is 20 to 30% faster on the short simple benchmarks in the
testsuite (30% for the ascii text).

Since the iterator will easily run out of bounds on invalid data, it is
now truly memory unsafe to reference non-UTF-8 data with &str, like we
have promised for a long time.

Before::

test str::bench::char_indicesator ... bench: 168 ns/iter (+/- 4)
test str::bench::char_indicesator_rev ... bench: 252 ns/iter (+/- 4)
test str::bench::char_iterator ... bench: 168 ns/iter (+/- 21)
test str::bench::char_iterator_ascii ... bench: 411 ns/iter (+/- 56)
test str::bench::char_iterator_rev ... bench: 256 ns/iter (+/- 5)

After::

test str::bench::char_indicesator ... bench: 148 ns/iter (+/- 4)
test str::bench::char_indicesator_rev ... bench: 163 ns/iter (+/- 5)
test str::bench::char_iterator ... bench: 135 ns/iter (+/- 4)
test str::bench::char_iterator_ascii ... bench: 294 ns/iter (+/- 6)
test str::bench::char_iterator_rev ... bench: 172 ns/iter (+/- 10)