|
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& >.
Chris
_____________________________________________
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:
if(m_initialized){
// 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.
Chris
------------------
#include <boost/optional.hpp>
typedef boost::optional<int> optional_int;
void deconstruct_boost_optional(optional_int& o){
o.~optional_int();
}
------------------
objdump -f -d -C -l boost_optional_deconstruction.o |c++filt
00000000 <deconstruct_boost_optional(boost::optional<int>&)>:
deconstruct_boost_optional(boost::optional<int>&)():
/home/eh2hite/chris/src_root/src/test/boost_optional_deconstruction.cpp:5
0: 8b 44 24 04 mov 0x4(%esp),%eax
boost::optional_detail::optional_base<int>::destroy()():
/usr/local/include/boost/optional/optional.hpp:407
4: 80 38 00 cmpb $0x0,(%eax)
7: 74 03 je c <deconstruct_boost_optional(boost::optional<int>&)+0xc>
boost::optional_detail::optional_base<int>::destroy_impl(mpl_::bool_<false>)():
/usr/local/include/boost/optional/optional.hpp:434
9: c6 00 00 movb $0x0,(%eax)
c: f3 c3 repz ret
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net