Boost logo

Boost Users :

Subject: Re: [Boost-users] ~optional<int> generates unnessesary code
From: Hite, Christopher (Christopher.Hite_at_[hidden])
Date: 2012-01-25 06:13:08

Nobody seems to want to bite here. I found more issues with optional which make it expensive.
1) When assigning it checks m_initialized to see if it needs to use the copy constructor or operator= . That also makes no sense for POD types.

2) has_trivial_copy< optional<int> >::value should be true, just like for std::pair<int,bool>.

3) has_trivial_destructor< optional<T> > should be true iff has_trivial_destructor<T>, just like for std::pair<T,bool>.

Generally I think all these problems could be addressed using these standard meta-functions to optimize where possible.

I'd be willing to put it some time. Who do I talk to? Am I posting to the wrong list?

Note checking has_trivial_destructor might take care of the complexity of optional<T&> since a has_trivial_destructor< T& >.


From: Hite, Christopher
Sent: Monday, January 23, 2012 12:42 PM
To: 'boost-users_at_[hidden]'
Subject: ~optional<int> generates unnessesary code

The problem is that m_initialized is checked and set during deconstruction. This generates a bunch of useless code for types with trivial deconstruction:

                // do nothing
                m_initialized = false;

The code is more than useless since it contains a branch. In my case I've got a big object I would expect to die without peep, branching and writing to memory for no reason.

The steps below show what gcc 4.6.0 generates. You'd expect the function to do nothing.

Boost optional has no obligation to maintain state during deconstruction (otherwise you'd need states for deconstructing,constructing). It's not responsible for the bytes left behind after deconstruction.

By the way boost::variant does do this correctly.



#include <boost/optional.hpp>

typedef boost::optional<int> optional_int;

void deconstruct_boost_optional(optional_int& o){


objdump -f -d -C -l boost_optional_deconstruction.o |c++filt

00000000 <deconstruct_boost_optional(boost::optional<int>&)>:
   0: 8b 44 24 04 mov 0x4(%esp),%eax
   4: 80 38 00 cmpb $0x0,(%eax)
   7: 74 03 je c <deconstruct_boost_optional(boost::optional<int>&)+0xc>
   9: c6 00 00 movb $0x0,(%eax)
   c: f3 c3 repz ret

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at