|
Boost Users : |
Subject: [Boost-users] has_trivial_copy for optimization
From: Hite, Christopher (Christopher.Hite_at_[hidden])
Date: 2010-09-15 10:28:43
I noticed poor performance coping a structure with a bunch of
optionals. The reason is the cost of checking if the optionals are set.
CPU's hate branching it breaks their pipelining.
if(that->a)
this->a=*that->a;
At the end of the day I just wrote a copy construct which does a
memcpy(), since the data types were just POD. It makes me a bit nervous
since my code might break with a different impl of opitonal.
Then I thought hey wouldn't it be cool if defined a metafunction that
specified memcpy/memmove are allowed? It looks like has_trivial_copy<>
fits the bill.
// Suggested code:
namespace boost{
template<typename T>
struct has_trivial_copy< boost::optional<T> > :
has_trivial_copy<T> {};
}//namespace boost
typedef boost::optional<int> OptionalInt;
BOOST_STATIC_ASSERT(( has_trivial_copy<OptionalInt>::value )); //fixed
thanks to code above
struct SomeOptionals{ OptionalInt a,b;};
BOOST_STATIC_ASSERT(( has_trivial_copy<SomeOptionals>::value )); //fails
The second test fails. It can't imagine how to make it work without
some compiler magic. I wonder if a compiler could use metafunctions to
know which optimizations were legal.
Similar meta function specializations could be written for
boost::variant and boost::fusion::map. I could imagine contains like
std::vector using this to do memmove() for example.
Chris
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