<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/95987>95987</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Failure to remove `alloca`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
`str::starts_with(char)` and `str::ends_with(char)` converts the 4-byte character codepoint into a 1-4 byte buffer on the stack, then compares against the start/end of the `str`'s bytes contents. LLVM is able to remove the stack allocation when the `char` is a single byte, but if it is 2 or more, it is not able:
https://godbolt.org/z/zjhanPx7j
```rust
#[no_mangle]
pub fn starts_with_char1(s: &str) -> bool {
s.starts_with('a')
}
#[no_mangle]
pub fn starts_with_str1(s: &str) -> bool {
s.starts_with("a")
}
#[no_mangle]
pub fn starts_with_char2(s: &str) -> bool {
s.starts_with('£')
}
#[no_mangle]
pub fn starts_with_str2(s: &str) -> bool {
s.ends_with("£")
}
#[no_mangle]
pub fn ends_with_char1(s: &str) -> bool {
s.ends_with('a')
}
#[no_mangle]
pub fn ends_with_str1(s: &str) -> bool {
s.ends_with("a")
}
#[no_mangle]
pub fn ends_with_char2(s: &str) -> bool {
s.ends_with('£')
}
#[no_mangle]
pub fn ends_with_str2(s: &str) -> bool {
s.ends_with("£")
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0lUtv2zAMxz8NfSESyPQzBx_aZrmsA3batZBtOVZrS4FEp2s__SAnXZNilzQdkAdM8fHTXzQlvddbo1QF2S1k60hO3FtXfR-VfNImqm37UkEuPDtIbiC58Swd-4dnzT1Q2fTSAa0gFyhNi6eOyrT_cmus2SvHHrlXmC7qF1YYlmXDymFjW7Wz2jBqwxYlxosUZ5966jrl0Jo50LNsnoDuwoPBxo476ZRHuZXaeH5zcQy0UaZF282mI14ugAo_p_WBh5Vhv8T7-18_UHuU9aCQLTo12r16L4dyGGwjWVuDz6HsMeW8uVzMoei12Q5qzh3w6olRd6g5rBJah6N188rBZCzP9YJkYg3i-Nsz73yw0QZos7VtbQdeWrcF2ryG72Mvzc_fxeMxKBeHj5s8H02UQHZr7MMoAxBk64N9N9XYGTw5xYfAHwOVoSAC5UEiWuECkm9YWzsgFLeHYEREvzzvAKBCAhXhdA-Fi_XpTi7g8HwVBkkg-gKMIAddIwfcEdwkX6bJJSyn71xQ40jyWVn-pru4R85BrumQd4YL--ODFld0x7kMnz-PqzvjTIv_1BdvoyRqq6RdJSsZqSou4rJI05TSqK_StGwpLlORNKTysstyKZqkpaLo1KrN00hXJCgVeVxSLAQlS5nXGXVUxKREnWQdpEKNUg_LYdiPYapF2vtJVatsVRbRIGs1-Pk6Ihq196pd2B3rUb_OszdgZ-vIVSF6UU9bD6kYtGf_no81D6raSD1M7nSWQy4OQzxscHJD9WHQau6netnYEWgTkh3_FjtnH1UTbpOZ1ANtDrD7iv4EAAD__5rkHqk">