Boost logo

Boost :

Subject: Re: [boost] expected/result/etc
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2016-02-11 12:57:13


On 2/11/2016 5:47 AM, Niall Douglas wrote:
> outcome<int> _foo; // contains some value
> int foo;
> if(_foo) foo=_foo.get();
> foo=_foo.get_or(5); // also works

I'm not convinced.

I ran a comparison of a trivial implementation of result with your test
code versus your implementation.
The results weren't pretty.

Here's my test code:
http://ideone.com/JYsSYc

Here's the results (VC2015 /O2 /Ob2)

Trivial:

int main()
{
00007FF7B7053530 sub rsp,48h
00007FF7B7053534 mov rax,qword ptr [__security_cookie
(07FF7B705D020h)]
00007FF7B705353B xor rax,rsp
00007FF7B705353E mov qword ptr [rsp+38h],rax
     result<int> _foo = bar();
00007FF7B7053543 lea rcx,[_foo]
00007FF7B7053548 call bar (07FF7B7051172h)
     int foo = 0;
00007FF7B705354D xor eax,eax
     if ( _foo ) {
00007FF7B705354F lea rcx,[instance (07FF7B705D000h)]
00007FF7B7053556 cmp qword ptr [rsp+30h],rcx
00007FF7B705355B cmove eax,dword ptr [_foo]
         foo = _foo.get();
     }
     return foo;
}
00007FF7B7053560 mov rcx,qword ptr [rsp+38h]
00007FF7B7053565 xor rcx,rsp
00007FF7B7053568 call __security_check_cookie (07FF7B70512A3h)
00007FF7B705356D add rsp,48h
00007FF7B7053571 ret

Boost.Outcome

int main()
{
00007FF6E25C2BB0 mov rax,rsp
00007FF6E25C2BB3 push rbp
00007FF6E25C2BB4 lea rbp,[rax-58h]
00007FF6E25C2BB8 sub rsp,150h
00007FF6E25C2BBF mov qword ptr [rbp-38h],0FFFFFFFFFFFFFFFEh
00007FF6E25C2BC7 mov qword ptr [rax+8],rbx
00007FF6E25C2BCB movaps xmmword ptr [rax-18h],xmm6
00007FF6E25C2BCF mov rax,qword ptr [__security_cookie
(07FF6E25CC010h)]
00007FF6E25C2BD6 xor rax,rsp
00007FF6E25C2BD9 mov qword ptr [rbp+30h],rax
     result<int> _foo = bar();
00007FF6E25C2BDD lea rcx,[rbp-50h]
00007FF6E25C2BE1 call bar (07FF6E25C109Bh)
00007FF6E25C2BE6 nop
     int foo = 0;
00007FF6E25C2BE7 xor ebx,ebx
     if ( _foo ) {
00007FF6E25C2BE9 movzx ecx,byte ptr [rbp-40h]
00007FF6E25C2BED cmp cl,1
00007FF6E25C2BF0 sete al
00007FF6E25C2BF3 test al,al
00007FF6E25C2BF5 je main+267h (07FF6E25C2E17h)
         foo = _foo.get();
00007FF6E25C2BFB test cl,cl
00007FF6E25C2BFD setne al
00007FF6E25C2C00 test al,al
00007FF6E25C2C02 jne main+119h (07FF6E25C2CC9h)
00007FF6E25C2C08 mov dword ptr [rsp+20h],2
00007FF6E25C2C10 call boost::outcome::v1_std_std::monad_category
(07FF6E25C10DCh)
00007FF6E25C2C15 mov qword ptr [rsp+28h],rax
00007FF6E25C2C1A movaps xmm0,xmmword ptr [rsp+20h]
00007FF6E25C2C1F movdqa xmmword ptr [rsp+20h],xmm0
00007FF6E25C2C25 lea rdx,[rbp-10h]
00007FF6E25C2C29 lea rcx,[rsp+20h]
         foo = _foo.get();
00007FF6E25C2C2E call std::error_code::message (07FF6E25C1190h)
00007FF6E25C2C33 cmp qword ptr [rax+18h],10h
00007FF6E25C2C38 jb main+8Dh (07FF6E25C2C3Dh)
00007FF6E25C2C3A mov rax,qword ptr [rax]
00007FF6E25C2C3D lea rcx,[std::exception::`vftable'
(07FF6E25C9D68h)]
00007FF6E25C2C44 mov qword ptr [rsp+58h],rcx
00007FF6E25C2C49 xor ecx,ecx
00007FF6E25C2C4B mov qword ptr [rsp+60h],rcx
00007FF6E25C2C50 mov qword ptr [rsp+68h],rcx
00007FF6E25C2C55 mov qword ptr [rbp-80h],rax
00007FF6E25C2C59 mov byte ptr [rbp-78h],1
00007FF6E25C2C5D lea rdx,[rsp+60h]
00007FF6E25C2C62 lea rcx,[rbp-80h]
00007FF6E25C2C66 call qword ptr [__imp___std_exception_copy
(07FF6E25CE1C0h)]
00007FF6E25C2C6C lea rax,[std::logic_error::`vftable'
(07FF6E25C9DA0h)]
00007FF6E25C2C73 mov qword ptr [rsp+58h],rax
00007FF6E25C2C78 mov r8,qword ptr [rbp+8]
00007FF6E25C2C7C cmp r8,10h
00007FF6E25C2C80 jb main+0E2h (07FF6E25C2C92h)
00007FF6E25C2C82 inc r8
00007FF6E25C2C85 mov rdx,qword ptr [rbp-10h]
00007FF6E25C2C89 lea rcx,[rbp-10h]
00007FF6E25C2C8D call std::_Wrap_alloc<std::allocator<char>
>::deallocate (07FF6E25C126Ch)
00007FF6E25C2C92 mov qword ptr [rbp+8],0Fh
00007FF6E25C2C9A mov qword ptr [rbp],rbx
00007FF6E25C2C9E mov byte ptr [rbp-10h],bl
00007FF6E25C2CA1 lea
rax,[boost::outcome::v1_std_std::monad_error::`vftable' (07FF6E25C9F08h)]
00007FF6E25C2CA8 mov qword ptr [rsp+58h],rax
00007FF6E25C2CAD movaps xmm0,xmmword ptr [rsp+20h]
00007FF6E25C2CB2 movups xmmword ptr [rsp+70h],xmm0
00007FF6E25C2CB7 lea
rdx,[_TI3?AVmonad_error_at_v1_std_std_at_outcome@boost@@ (07FF6E25CBAB0h)]
00007FF6E25C2CBE lea rcx,[rsp+58h]
00007FF6E25C2CC3 call _CxxThrowException (07FF6E25C4DDEh)
00007FF6E25C2CC8 int 3
00007FF6E25C2CC9 cmp cl,2
00007FF6E25C2CCC sete al
00007FF6E25C2CCF test al,al
00007FF6E25C2CD1 jne main+137h (07FF6E25C2CE7h)
00007FF6E25C2CD3 sub cl,2
00007FF6E25C2CD6 cmp cl,1
00007FF6E25C2CD9 ja main+264h (07FF6E25C2E14h)
00007FF6E25C2CDF test al,al
00007FF6E25C2CE1 je main+264h (07FF6E25C2E14h)
00007FF6E25C2CE7 mov qword ptr [rbp-18h],0Fh
00007FF6E25C2CEF mov qword ptr [rbp-20h],rbx
00007FF6E25C2CF3 mov byte ptr [rbp-30h],bl
00007FF6E25C2CF6 xor r8d,r8d
00007FF6E25C2CF9 lea rdx,[string "" (07FF6E25C9E5Ch)]
00007FF6E25C2D00 lea rcx,[rbp-30h]
00007FF6E25C2D04 call
std::basic_string<char,std::char_traits<char>,std::allocator<char>
>::assign (07FF6E25C1037h)
00007FF6E25C2D09 nop
00007FF6E25C2D0A mov qword ptr [rbp-58h],0Fh
00007FF6E25C2D12 mov qword ptr [rbp-60h],rbx
00007FF6E25C2D16 mov byte ptr [rbp-70h],0
00007FF6E25C2D1A or r9,0FFFFFFFFFFFFFFFFh
00007FF6E25C2D1E xor r8d,r8d
00007FF6E25C2D21 lea rdx,[rbp-30h]
00007FF6E25C2D25 lea rcx,[rbp-70h]
00007FF6E25C2D29 call
std::basic_string<char,std::char_traits<char>,std::allocator<char>
>::assign (07FF6E25C1159h)
00007FF6E25C2D2E movups xmm6,xmmword ptr [rbp-50h]
00007FF6E25C2D32 movups xmmword ptr [rsp+20h],xmm6
00007FF6E25C2D37 lea r8,[rbp-70h]
00007FF6E25C2D3B lea rdx,[rsp+20h]
00007FF6E25C2D40 lea rcx,[rbp+10h]
00007FF6E25C2D44 call std::_System_error::_Makestr
(07FF6E25C11E0h)
00007FF6E25C2D49 cmp qword ptr [rax+18h],10h
00007FF6E25C2D4E jb main+1A3h (07FF6E25C2D53h)
00007FF6E25C2D50 mov rax,qword ptr [rax]
00007FF6E25C2D53 lea rcx,[std::exception::`vftable'
(07FF6E25C9D68h)]
00007FF6E25C2D5A mov qword ptr [rsp+30h],rcx
00007FF6E25C2D5F xor ecx,ecx
00007FF6E25C2D61 mov qword ptr [rsp+38h],rcx
00007FF6E25C2D66 mov qword ptr [rsp+40h],rcx
00007FF6E25C2D6B mov qword ptr [rsp+20h],rax
00007FF6E25C2D70 mov byte ptr [rsp+28h],1
00007FF6E25C2D75 lea rdx,[rsp+38h]
00007FF6E25C2D7A lea rcx,[rsp+20h]
00007FF6E25C2D7F call qword ptr [__imp___std_exception_copy
(07FF6E25CE1C0h)]
00007FF6E25C2D85 lea rax,[std::runtime_error::`vftable'
(07FF6E25C9DC0h)]
00007FF6E25C2D8C mov qword ptr [rsp+30h],rax
00007FF6E25C2D91 mov r8,qword ptr [rbp+28h]
00007FF6E25C2D95 cmp r8,10h
00007FF6E25C2D99 jb main+1FBh (07FF6E25C2DABh)
00007FF6E25C2D9B inc r8
00007FF6E25C2D9E mov rdx,qword ptr [rbp+10h]
00007FF6E25C2DA2 lea rcx,[rbp+10h]
00007FF6E25C2DA6 call std::_Wrap_alloc<std::allocator<char>
>::deallocate (07FF6E25C126Ch)
00007FF6E25C2DAB mov qword ptr [rbp+28h],0Fh
00007FF6E25C2DB3 mov qword ptr [rbp+20h],rbx
00007FF6E25C2DB7 mov byte ptr [rbp+10h],0
00007FF6E25C2DBB lea rax,[std::_System_error::`vftable'
(07FF6E25C9E28h)]
00007FF6E25C2DC2 mov qword ptr [rsp+30h],rax
00007FF6E25C2DC7 movups xmmword ptr [rsp+48h],xmm6
00007FF6E25C2DCC mov r8,qword ptr [rbp-18h]
00007FF6E25C2DD0 cmp r8,10h
00007FF6E25C2DD4 jb main+236h (07FF6E25C2DE6h)
00007FF6E25C2DD6 inc r8
00007FF6E25C2DD9 mov rdx,qword ptr [rbp-30h]
00007FF6E25C2DDD lea rcx,[rbp-30h]
00007FF6E25C2DE1 call std::_Wrap_alloc<std::allocator<char>
>::deallocate (07FF6E25C126Ch)
00007FF6E25C2DE6 mov qword ptr [rbp-18h],0Fh
00007FF6E25C2DEE mov qword ptr [rbp-20h],rbx
00007FF6E25C2DF2 mov byte ptr [rbp-30h],0
00007FF6E25C2DF6 lea rax,[std::system_error::`vftable'
(07FF6E25C9E48h)]
00007FF6E25C2DFD mov qword ptr [rsp+30h],rax
00007FF6E25C2E02 lea rdx,[_TI4?AVsystem_error_at_std@@
(07FF6E25CB9C8h)]
00007FF6E25C2E09 lea rcx,[rsp+30h]
00007FF6E25C2E0E call _CxxThrowException (07FF6E25C4DDEh)
00007FF6E25C2E13 int 3
00007FF6E25C2E14 mov ebx,dword ptr [rbp-50h]
     }
     return foo;
00007FF6E25C2E17 mov eax,ebx
}
00007FF6E25C2E19 mov rcx,qword ptr [rbp+30h]
00007FF6E25C2E1D xor rcx,rsp
00007FF6E25C2E20 call __security_check_cookie (07FF6E25C12DFh)
00007FF6E25C2E25 mov rbx,qword ptr [rsp+160h]
00007FF6E25C2E2D movaps xmm6,xmmword ptr [rsp+140h]
00007FF6E25C2E35 add rsp,150h
00007FF6E25C2E3C pop rbp
00007FF6E25C2E3D ret


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk