9

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.

Community
  • 1
  • 1
amit kumar
  • 20,438
  • 23
  • 90
  • 126

2 Answers2

1

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.

Hugo Corrá
  • 14,546
  • 3
  • 27
  • 39
1

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

AlexT
  • 1,413
  • 11
  • 11