mirror of
https://github.com/allankoechke/rust.git
synced 2026-11-30 11:05:36 +00:00
perf: drop error in `unwrap_failed` instead of `unwrap` Context: https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/40-byte.20copy.20of.20unwrapped.20Result.3CT.2C.20.28.29.3E.20payload.20survives.20-O3/with/630360641 This PR changes `Result::unwrap` to pass the error by value into `unwrap_failed` rather than by reference. This makes `unwrap` no longer responsible for dropping the error after `unwrap_failed` panics, which reduces code size and compilation times and enables MIR-inlining of `unwrap`. The downside is that it introduces another function that must be monomorphized per error type (though the formatting part stays polymorphic); but it turned out to be a net perf win. I tried keeping the `unwrap_failed` fully polymorphic by passing the error as `&mut ManuallyDrop<dyn Debug>`, but this turned out to be worse for performance. [The first perf run](https://github.com/rust-lang/rust/pull/164059#issuecomment-6090859813) matches the current state of this PR ([the second one](https://github.com/rust-lang/rust/pull/164059#issuecomment-6093489095) is for the ManuallyDrop implementation that turned out to be slower). Generated code comparison (with an example of an error that has a destructor so that the landing pad shows up in the assembly post-LLVM): https://rust.godbolt.org/z/ajjYMTc7Y Old: ```asm push r14 push rbx sub rsp, 24 cmp qword ptr [rsi], -1 je .LBB5_1 mov rax, qword ptr [rsi + 16] mov qword ptr [rdi + 16], rax movups xmm0, xmmword ptr [rsi] movups xmmword ptr [rdi], xmm0 mov rax, rdi add rsp, 24 pop rbx pop r14 ret .LBB5_1: mov eax, dword ptr [rsi + 8] mov dword ptr [rsp + 4], eax lea rdi, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.1] lea rcx, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.0] lea r8, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.8] lea r14, [rsp + 4] mov esi, 43 mov rdx, r14 call qword ptr [rip + core[a9090a07a7fa7517]::result::unwrap_failed@GOTPCREL] ud2 mov rbx, rax mov qword ptr [rsp + 8], r14 mov rax, qword ptr [rip + <i32 as core[a9090a07a7fa7517]::fmt::Display>::fmt@GOTPCREL] mov qword ptr [rsp + 16], rax lea rdi, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.11] lea rsi, [rsp + 8] call qword ptr [rip + std[c29fa2d9dbea2b0e]::io::stdio::_print@GOTPCREL] mov rdi, rbx call _Unwind_Resume@PLT call qword ptr [rip + core[a9090a07a7fa7517]::panicking::panic_in_cleanup@GOTPCREL] ``` New: ```asm example[32c4274de85e6574]::generic::unwrap: cmp qword ptr [rsi], -1 je .LBB7_2 mov rax, qword ptr [rsi + 16] mov qword ptr [rdi + 16], rax movups xmm0, xmmword ptr [rsi] movups xmmword ptr [rdi], xmm0 mov rax, rdi ret .LBB7_2: push rax mov edx, dword ptr [rsi + 8] lea rdi, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.1] lea rcx, [rip + .Lanon.99b5833966418e083b8bdda9f0bfd3cb.10] mov esi, 43 call example[32c4274de85e6574]::generic::unwrap_failed::<example[32c4274de85e6574]::Error> ```
The files here use the LLVM FileCheck framework, documented at https://llvm.org/docs/CommandGuide/FileCheck.html.
One extension worth noting is the use of revisions as custom prefixes for FileCheck. If your codegen test has different behavior based on the chosen target or different compiler flags that you want to exercise, you can use a revisions annotation, like so:
// revisions: aaa bbb
// [bbb] compile-flags: -Csomething=true
After specifying those variations, you can write different expected, or
explicitly unexpected output by using <prefix>-SAME: and <prefix>-NOT:,
like so:
// CHECK: expected code
// aaa-SAME: emitted-only-for-aaa
// aaa-NOT: emitted-only-for-bbb
// bbb-NOT: emitted-only-for-aaa
// bbb-SAME: emitted-only-for-bbb
If multiple test revisions want to share some annotations, you can use a setup like this:
// revisions: aaa bbb ccc
// [bbb] filecheck-flags: --check-prefix=both
// [ccc] filecheck-flags: --check-prefix=both
Now aaa:, bbb:, ccc: annotations are only checked for those specific revisions,
and both: annotations are checked for revisions bbb and ccc.