0

I have found it useful to be able to serialize user-defined structs. I usually use something like

#include <msgpack.hpp>
struct MyStruct {
     int val;
     MSGPACK_DEFINE_MAP(val);
};

This will then get included anywhere that needs it. I later noticed long compile times. When I ran g++ with the -H flag, I found that around 1/3 of the included files are msgpack related.

Is there any way to easily use msgpack while avoiding the headers getting re-included everywhere and bloating compile times?

sardine
  • 13
  • 5
  • 1
    Precompiled headers are usually the answer to big headers. If you only use msgpack in a small number of translation units and don't care much about undefined behavior because of ODR violations then you can #ifdef the macro and the msgpack include. – PeterT Oct 09 '17 at 20:07
  • I'll give pre-compiled headers a whirl. I see.. so all the build targets that need msgpack would define some flag like USE_MSGPACK, and i'd have these ifdef's everywhere? – sardine Oct 09 '17 at 20:45
  • If msgpack must be used within the class definition itself, avoid msgpack at all costs. Look for something that can be used in .cpp files. – o11c Oct 09 '17 at 22:05
  • Then, if the few remaining .cpp files are slow, compile *just* those files with exactly `-O1`, which is generally same-or-faster-to-compile than either `-O0` or `-O2` – o11c Oct 09 '17 at 22:07
  • As Jens pointed out, there is a non-intrusive way to use msgpack, but it requires writing lots of boilerplate that the MSGPACK_DEFINE macros do for you. There's an unfortunate trade-off between code complexity and compile time. – sardine Oct 11 '17 at 15:22

1 Answers1

0

I am not very familiar with msgpack, but the first idea that came to my mind is to pimpl the class. Then, I was wondering if pimpl can be used for serialization and had a look at the msgpack docs.

There is a non-intrusive approach for defining the msgpack mapper. You could put this code in a separate MyStruct_serialize.hpp file and include this only where you actually serialize your class. This way, most of the code should not load any msgpack headers.

Jens
  • 9,058
  • 2
  • 26
  • 43
  • I see, the non-intrusive approach would do what I want. Unfortunately it requires writing lots of boilerplate. – sardine Oct 11 '17 at 15:20