c++ - 在boost序列化中设置模板类的跟踪特征以减少内存消耗

标签 c++ boost boost-serialization memory-profiling

如这个link为了定义模板类的特征,我们应该手动定义它,或者从特征类中提取我们的类。但我想自动完成这个过程,因此受到BOOST_CLASS_TRACKING的启发。我写了下面的代码:

#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>)

我在代码中使用了这个宏,但现在我不确定这个宏是否会阻止类跟踪。 我有一个大数据结构,我想在序列化过程中消耗更少的内存。通过使用 callgrind 检查我的程序我发现大部分new()序列化库中的调用来自名为 save_pointer 的函数在文件 basic_oarchive.hpp它存储了跟踪对象的指针映射,我希望将所有类更改为 never_track内存消耗显着减少。但没有发生重大变化。

我的宏有问题吗?或者序列化的内存消耗与对象的跟踪无关? 有没有办法知道某个类的跟踪特征是否已设置?

编辑:

我的项目简而言之是一个特里树,每个节点都是一个抽象类的指针,并且有指向其子节点的指针。如果我不禁用指针跟踪,所有这些节点都会保存在 boost 序列化库的映射上,并且在序列化期间内存会乘以两倍。

更新:

我放在这里的宏效果很好。但是为了禁用跟踪,您必须注意到库有许多内部指针跟踪它们。例如,在我的例子中,有很多指向 pair<const Key, Value> 的指针。这是许多 STL 或其他容器的内部指针。通过禁用所有这些,内存消耗会显着减少。

最佳答案

更新

OP 自 posted the synthetic benchmark这确实表明了他正在尝试测量的东西。

我在 Massif 下运行了两次:左边只是构建了一棵大树,右边也序列化了它:https://gist.github.com/sehe/5f060a3daccfdff3178c#file-sbs-txt

enter image description here

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

为了进行比较,启用跟踪时:https://gist.github.com/8d3e5dba7b124a750b9b

enter image description here

结论

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

是的。显然是这样。


旧答案的原始脚注:

¹ 不,它通常不会是内存量的两倍 - 这将需要一种非常特定的数据集,并且负载与特里节点大小的比率非常低

关于c++ - 在boost序列化中设置模板类的跟踪特征以减少内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391792/

相关文章:

c++ - grpc & protobuf -- 错误:std::result_of<> 中没有名为 'type' 的类型

c++ - Qt 小部件项目中的 Ui 命名空间

c++ - 从 native C++ 重构代码位的选项?

C++ BOOST undefined reference `boost::filesystem::detail::copy_file

c++ - 调用AUEffectBase::Render时如何确定激活的输入总线?

c++ - boost::program_options 附加具有相似名称的 vector 选项

c++ - 无法将 boost 库链接到共享库

c++ - boost 空 std::forward_list 的序列化

c++ - boost序列化实际上是如何保存const对象的

c++ - 使用映射 boost 二进制序列化并在序列化时加倍崩溃