Evidently hold_any
has better performance than boost::any
. How does it manage to do this?
Edit: Thanks to Mat's comment, I found an answer by hkaiser about hold_any
at another question but it lacks details. A more detailed answer would be welcome.
I think one of the reasons is because boost::hold_any uses a template metaprogramming approach whilst boost::any uses an inheritance approach.
Internally, boost::spirit::hold_any stores the "value" using a void* and uses another object to keep track of the data type info:
>> boost/spirit/home/support/detail/hold_any.hpp
template <typename Char>
class basic_hold_any
{
....
spirit::detail::fxn_ptr_table<Char>* table;
void* object;
...
}
boost::any stores the "value" using an holder, and it doesn't need another object to keep track of the data type info. The holder is inherited from placeholder and consequently have the inheritance drawbacks.
>> boost/any.hpp
class any
{
...
placeholder * content;
...
}
class placeholder
template<typename ValueType>
class holder : public placeholder
{
...
ValueType held;
...
}
...the perfomance difference is much more about calling constructors and destructors, but even for casting, boost::spirit::hold_any should be faster.
hold_any does two optimization: 1. For small objects it does't allocate memory for object holder but stores it directly in pointer memory; 2. It doesn't use RTTI type comparison (which is slow) but use its own type table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With