Is there a way to get information on a boost::flyweight internal container?

624 views Asked by At

Using boost::flyweight is supposed to help me save memory. I am looking for way to get a quantitative measurement on the effectiveness of the solution.

Is there a way to get the size() of the internal container? If its a hash based flyweight, is there a way to get info on bucket status? hash collisions, etc?

Any pointers will be appreciated.

2

There are 2 answers

1
Joaquín M López Muñoz On

Have a look at the source code of boost::flyweight::hashed_factory_class: you can just clone the code to derive your own user-defined factory and provide public (preferrable const) access to the internal container.

3
Joaquín M López Muñoz On

My previous answer didn't provide sufficient detail, and the solution is really not so simple. This is a complete snippet showing how to do it:

#include <boost/flyweight/factory_tag.hpp>
#include <boost/flyweight/hashed_factory_fwd.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/mpl/aux_/lambda_support.hpp>
#include <boost/mpl/if.hpp>

class bucket_query
{
public:
  typedef std::size_t size_type;

  virtual size_type bucket_count()const=0;
  virtual size_type max_bucket_count()const=0;
  virtual size_type bucket_size(size_type n)const=0;
};

static bucket_query* bucket_query_ptr=0;

template<
  typename Entry,typename Key,
  typename Hash=boost::mpl::na,typename Pred=boost::mpl::na,
  typename Allocator=boost::mpl::na
>
class accessible_hashed_factory_class:
  public boost::flyweights::factory_marker,
  public bucket_query
{
  struct index_list:
    boost::mpl::vector1<
      boost::multi_index::hashed_unique<
        boost::multi_index::identity<Entry>,
        typename boost::mpl::if_<
          boost::mpl::is_na<Hash>,
          boost::hash<Key>,
          Hash
        >::type,
        typename boost::mpl::if_<
          boost::mpl::is_na<Pred>,
          std::equal_to<Key>,
          Pred
        >::type
      >
    >
  {};

  typedef boost::multi_index::multi_index_container<
    Entry,
    index_list,
    typename boost::mpl::if_<
      boost::mpl::is_na<Allocator>,
      std::allocator<Entry>,
      Allocator
    >::type
  > container_type;

public:
  typedef const Entry* handle_type;

  accessible_hashed_factory_class(){bucket_query_ptr=this;}

  handle_type insert(const Entry& x)
  {
    return &*cont.insert(x).first;
  }

  void erase(handle_type h)
  {
    cont.erase(cont.iterator_to(*h));
  }

  static const Entry& entry(handle_type h){return *h;}

  typedef std::size_t size_type;

  virtual size_type bucket_count()const{return cont.bucket_count();}
  virtual size_type max_bucket_count()const{return cont.max_bucket_count();}
  virtual size_type bucket_size(size_type n)const{return cont.bucket_size(n);}

private:  
  container_type cont;

public:
  typedef accessible_hashed_factory_class type;
  BOOST_MPL_AUX_LAMBDA_SUPPORT(
    5,accessible_hashed_factory_class,(Entry,Key,Hash,Pred,Allocator))
};

template<
  typename Hash=boost::mpl::na,typename Pred=boost::mpl::na,
  typename Allocator=boost::mpl::na
  BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION
>
struct accessible_hashed_factory:boost::flyweights::factory_marker
{
  template<typename Entry,typename Key>
  struct apply:
    boost::mpl::apply2<
      accessible_hashed_factory_class<
        boost::mpl::_1,boost::mpl::_2,Hash,Pred,Allocator
      >,
      Entry,Key
    >
  {};
};

/* testing */

#include <boost/flyweight.hpp>
#include <iostream>
#include <string>

int main()
{
  typedef boost::flyweight<std::string,accessible_hashed_factory<> > string_fw;

  string_fw s1("hello"),s2("hello"),s3("bye");

  std::cout<<"number of buckets: "<<bucket_query_ptr->bucket_count()<<std::endl;
}

The idea is: accessible_hashed_factory_class autoregisters itself via bucket_query_ptr exposing an interface for consulting bucket count etc. (bucket_query) that you can adapt and extend according to your needs. The solution is far from elegant but might solve your problem.