rust-lang/rust — pull request #16498 by Kimundi (2014-08-14 17:24:59+00:00)
Title: Improved code generation for `char`s UTF-8/UTF-16 encoding methods

The first commit improves code generation through a few changes:
- The `#[inline]` attributes allow llvm to constant fold the encoding step away in certain situations. For example, code like this changes from a call to `encode_utf8` in a inner loop to the pushing of a byte constant:

```rust
let mut s = String::new();
for _ in range(0u, 21) {
s.push_char('a');
}
```
- Both methods changed their semantic from causing run time failure if the target buffer is not large enough to returning 0 instead. This makes llvm no longer emit code for causing failure for these methods.
- A few debug `assert!()` calls got changed to actual `debug_assert!()` calls in order to improve optimized code generation.

The second commit is optional. It changes the methods from regular indexing with the `dst[i]` syntax to unsafe indexing with `dst.unsafe_mut_ref(i)`. This does not change code generation directly - in both cases llvm is smart enough to see that there can never be an out-of-bounds access. But it makes it emit a `nounwind` attribute for the function.
However, I'm not sure whether that is a real improvement, so if there is any objection to this I'll remove the commit.

This changes how the methods behave on a too small buffer, so this is a

[breaking-change]