fastest c++ serialization?

后端 未结 11 1491
自闭症患者
自闭症患者 2021-02-18 17:57

Good morning all,

I\'m searching for a very fast binary serialization technique for c++. I only need to serialize data contained in objects (no pointers etc.). I\'d like

相关标签:
11条回答
  • 2021-02-18 18:20

    There are just very few real-life cases where that matters at all. You only ever serialize to make your objects compatible with some kind of external resource. Disk, network, etcetera. The code that transmits the serialized data on the resource is always orders of magnitude slower then the code needed to serialize the object. If you make the serialization code twice as fast, you've made the overall operation no more than 0.5% faster, give or take. That is worth neither the risk nor the effort.

    Measure three times, cut once.

    0 讨论(0)
  • 2021-02-18 18:32

    The C++ Middleware Writer is an online alternative to serialization libraries. In some cases it is faster than the serialization library in Boost.

    0 讨论(0)
  • 2021-02-18 18:32

    If you're on a Unix system, mmap on the file is the way to do what you want to do.

    See http://msdn.microsoft.com/en-us/library/aa366556(VS.85).aspx for an equivalent on windows.

    0 讨论(0)
  • 2021-02-18 18:33

    Is there any way you can take advantage of things that stay the same?

    I mean, you are just trying to run through "test.c.dat" as fast as you possibly can, right? Can you take advantage of the fact that the file does not change between your serialization attempts? If you are trying to serialize the same file, over and over again, you can optimize based on this. I can make the first serialization attempt take the same amount of time as yours, plus a tiny bit extra for another check, and then if you try and run the serialization again on the same input, I can make my second run go much faster than the first time.

    I understand that this may just be a carefully crafted example, but you seem to be focused on making the language accomplish your task as quickly as possible, instead of asking the question of "do I need to accomplish this again?" What is the context of this approach?

    I hope this is helpful.

    -Brian J. Stinar-

    0 讨论(0)
  • 2021-02-18 18:34

    Both your C and your C++ code will probably be dominated (in time) by file I/O. I would recommend using memory mapped files when writing your data and leave the I/O buffering to the operating system. Boost.Interprocess could be an alternative.

    0 讨论(0)
  • 2021-02-18 18:36

    Well, if you want the fastest serialization possible, then you can just write your own serialization class and give it methods to serialize each of the POD types.

    The less safety you bring in, the faster it'll run and the harder it'll be to debug, however there is only a fixed number of built-in, so you could enumerate them.

    class Buffer
    {
    public:
      inline Buffer& operator<<(int i); // etc...
    private:
      std::deque<unsigned char> mData;
    };
    

    I must admit I don't understand your problem:

    • What do you actually want to do with the serialized message ?
    • Are you saving it for later ?
    • Do you have to worry about forward / backward compatibility ?

    There might be better approaches that serialization.

    0 讨论(0)
提交回复
热议问题