在boost序列化中设置模板类的跟踪特性,以减少内存消耗 [英] Set tracking traits of template class in boost serialization to reduce memory consumption
问题描述
由于此
请注意内存使用情况基本上完全相同
为了比较,启用跟踪时:
结论
Q。 我在我的代码中使用了这个宏,但现在我不确定这个宏是否阻止类的跟踪。
是的。
来自旧答案的原始脚注:
¹不会是通常是内存量的两倍 - 这将需要一个非常具体的数据集,其有效负载到特里节点大小比率非常低
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:
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.
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
这篇关于在boost序列化中设置模板类的跟踪特性,以减少内存消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!