lucacerone
lucacerone

Reputation: 10109

Efficient ways to save and load data from C++ simulation

I would like to know which are the best way to save and load C++ data. I am mostly interested in saving classes and matrices (not sparse) I use in my simulations. Now I just save them as txt files, but if I add a member to a class I then have to modify the function that loads the data (it has to parse and check for the value in the txt file), that I think is not ideal.

What would you recommend in general? (p.s. as I'd like to release my code I'd really like to use only standard c++ or libraries that can be redistributed).

Upvotes: 1

Views: 2213

Answers (3)

isaac
isaac

Reputation: 31

The C++ Middleware Writer automates the creation of marshalling functions. When you add a member to a class, it updates the marshalling functions for you.

Upvotes: 0

Jonathan Seng
Jonathan Seng

Reputation: 1219

Given that performance is not your concern -- which is a critical part of the answer -- the Boost Serialization library is a great answer.

The link in the comment leads to the documentation. Read the tutorial (which is overkill for what you are initially wanting, but well worth it).

Finally, since you have mostly array matrices, try to encapsulate the entire process of save and load so that should you need to change it later, you are writing a new implementatio and choosing between the exisiting. I expend the eddedmtime for the smarts of Boost Serialization would not be great; however, you might find a future requirement moves you to something else or multiple something elses.

Upvotes: 0

Jonathan Seng
Jonathan Seng

Reputation: 1219

In this case, there is no "best." What is best for you is highly dependent upon your situation. But, lets have an example to get you thinking about your details and how deep this rabbit hole can go.

If you absolutely positively must have the fastest save possible without question (and you're willing to pay the price), you can define your own memory management to put all objects into a contiguous array of a common type (such as integers). This allows you to write that array to disk as binary data very rapidly. You might need this in a simulation that uses threads efficiently to load every core/processor to run at real time.

Why is a rather horrible solution? Because it takes a LOT of work and runs many risks for problems in the name of "optimization."

  1. It requires you to build your own memory management (operator new() and operator delete()) which may need to be thread safe.
  2. If you try to load from this array, you will have to placement new all objects with a unique non-modifying constructor in order to ensure all virtual pointers are set properly. Oh, and you have to track the type of each address to now how to do this.
  3. For portability with other systems and between versions of the binary, you will need to have utilities to convert from the binary format to something generic enough to be cross platform (including repopulating pointers to other objects).

I have done this. It was highly unpleasant. I have no doubt there are still problems with it and I have only listed a few here. But, it was very, very fast and very, very, very problematic.

You must design to your needs. Generally, the first need is "Make it work." Don't care about efficiency, just about something that accurately persists and that you have the information known and accessible at some point to do it. Also, you should encapsulate the process of saving and loading. Then, if the need "Make it better" steps in, you should be able to change that one bit of code and the rest should work. You might even make the saving format selectable on user needs instead of your needs which you must assume for all users.

Given all the assumptions, pros and cons listed, you should be able to elaborate your particular needs for this question.

Upvotes: 1

Related Questions