Allow memcmp for more array comparisons#91766
Conversation
This way comparing `[NonZeroU8; 8]` is just as fast as comparing `[u8; 8]`.
|
r? @yaahc (rust-highfive has picked a reviewer for you, use r? to override) |
|
Very fancy, particularly appreciate the comments. @bors r+ |
|
📌 Commit 24affba has been approved by |
|
Since it's a specialization trait it should be possible to extend this to nested arrays/slices of arrays. |
|
@bors rollup=never might affect performance |
|
☀️ Test successful - checks-actions |
|
Finished benchmarking commit (83b32f2): comparison url. Summary: This change led to very large relevant mixed results 🤷 in compiler performance.
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression |
…t-slices, r=dtolnay Do array-slice equality via array equality, rather than always via slices ~~Draft because it needs a rebase after rust-lang#91766 eventually gets through bors.~~ This enables the optimizations from rust-lang#85828 to be used for array-to-slice comparisons too, not just array-to-array. For example, <https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=5f9ba69b3d5825a782f897c830d3a6aa> ```rust pub fn demo(x: &[u8], y: [u8; 4]) -> bool { *x == y } ``` Currently writes the array to stack for no reason: ```nasm sub rsp, 4 mov dword ptr [rsp], edx cmp rsi, 4 jne .LBB0_1 mov eax, dword ptr [rdi] cmp eax, dword ptr [rsp] sete al add rsp, 4 ret .LBB0_1: xor eax, eax add rsp, 4 ret ``` Whereas with the change in this PR it just compares it directly: ```nasm cmp rsi, 4 jne .LBB1_1 cmp dword ptr [rdi], edx sete al ret .LBB1_1: xor eax, eax ret ```
This way comparing
[NonZeroU8; 8]is just as fast as comparing[u8; 8].