Set tracking traits of template class in boost serialization to reduce memory consumption

时光毁灭记忆、已成空白 提交于 2019-12-12 08:17:26

问题


As this link stated for defining traits for a template class we should define it manually or we extract our class from the trait class. But I want to make this process automatically, for this reason inspired from BOOST_CLASS_TRACKING I wrote the blow code:

#include<boost/preprocessor/tuple/enum.hpp>

...

#define FOO_CLASS_TRACKING(E, PARAMETER_TUPLE, ...)           \
  namespace boost {                                             \
  namespace serialization {                                     \
  template<BOOST_PP_TUPLE_ENUM(PARAMETER_TUPLE)>                \
  struct tracking_level< __VA_ARGS__ >                          \
  {                                                             \
    typedef mpl::integral_c_tag tag;                            \
    typedef mpl::int_< E> type;                                 \
    BOOST_STATIC_CONSTANT(                                      \
                          int,                                  \
                          value = tracking_level::type::value   \
                                             );                 \
    /* tracking for a class  */                                 \
    BOOST_STATIC_ASSERT((                                       \
                         mpl::greater<                          \
                         /* that is a prmitive */               \
                         implementation_level< __VA_ARGS__ >,   \
                         mpl::int_<primitive_type>              \
                         >::value                               \
                                             ));                \
  };                                                            \
  }}

// which used like this
FOO_CLASS_TRACKING(boost::serialization::track_never, (typename Key, typename Value), Foo<Key, Value>)

I used this macro in my code, but now I am not sure whether this macro prevent the class from tracking or not. I have a big data structure and I want to consume less memory during serialization. By checking my program using callgrind I found that most of new() call in serialization lib is from a function named save_pointer in file basic_oarchive.hpp which stores a map of pointers to track objects, I expected by changing all classes to never_track memory consumption reduces significantly. But no significant change was happened.

Does my macro have a problem? or memory consumption of serialization does not relate to tracking of objects? Is there any way to find that tracking traits of a class was set or not?

Edit:

My project in brief is a trie that each node is a pointer of an abstract class and has pointer to its children. If I do not disable tracking of pointers all these nodes save on a map of boost serialization library and memory multiplies by two during serialization.

Update:

The macro I put here works well. But for disabling tracking you must notice that there are many internal pointer that the library tracks them. For example in my case there was many pointer to pair<const Key, Value> which is the internal pointer of many stl or other containers. By disabling all of them memory consumption reduces significantly.


回答1:


UPDATE

OP has since posted the synthetic benchmark that does show the thing he is trying to measure.

I ran it under Massif, twice: on the left just building a large Tree, and on the right also serializing it: https://gist.github.com/sehe/5f060a3daccfdff3178c#file-sbs-txt

Note how memory usage is basically exactly identical: object tracking is not an issue here

For comparison, when tracking is enabled: https://gist.github.com/8d3e5dba7b124a750b9b

Conclusion

Q. I used this macro in my code, but now I am not sure whether this macro prevent the class from tracking or not.

Yes. It clearly does.


Original footnote from old answer:

¹ No it would not usually be double the amount of memory - that would take a very specific kind of data set with a very low payload-to-trie-node size ratio



来源:https://stackoverflow.com/questions/35391792/set-tracking-traits-of-template-class-in-boost-serialization-to-reduce-memory-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!