3

Let's say I have a process written in C++ that has many instances of class A.

class A {
   std::vector<std::vector<short>> tab;
   /* other random data */
}

Once created, these objects should be accessed (read-only) by other processes of a different application, also in C++.

I would like to avoid at all cost making copies or moving the objects as it would significantly increase the memory consumption and probably take more time.

A 'simple' portable solution would be to serialize the objects in a shared memory and then when asked for the data, the process would just give the location of the various instances of class A in memory, and the second process would deserialize them before being able to read the data. That means that we would create a copy each time a process wants to read the data. This is what I would like to avoid.

Given that both processes are written in C++ and both know the definition of the class A, is it possible to avoid the serialization and therefore copy/displacement of the data ? Of course it would not be portable anymore but it does not need to be.

Can a simple static_cast through the shared memory allow the second process to read the data in it as its own without making any processing of any kind therefore costing no time and no additional memory at all ?

If not, is there a simpler form of serialization adding just an overhead that would allow the second process to understand and read the data without having to make a copy ?

Norgannon
  • 487
  • 4
  • 16
  • 4
    1. Absolutely no, every process has its own address space. 2. Copy memory-style serialization can be used only for POD data, don't even think about this when using vectors, strings etc. – Alex F Jun 26 '18 at 09:36
  • 1
    Then, is there absolutely no way to avoid making serialization (and therefore copies) when working with different processes ? – Norgannon Jun 26 '18 at 09:40
  • Perhaps you're looking for [Memory Mapped Files](https://msdn.microsoft.com/en-us/library/ms810613.aspx) ? – babu646 Jun 26 '18 at 09:49
  • @IanA.B.King I did not consider memory mapped files. If I understand properly, it is possible to create a memory mapped file based on each instance of class A. Then, when a second process wants to read the data, would it need to copy the data ? If so, it looks to me as just a different way to marshall the data no different than serialization. – Norgannon Jun 26 '18 at 10:01
  • @Norgannon have you even looked at it? Look at [some of my examples](https://stackoverflow.com/search?tab=votes&q=user%3a85371%20segment_manager%20vector) using boost. But just searching and reading would be a good start. – sehe Jun 26 '18 at 10:55
  • @sehe I looked at [this example](https://stackoverflow.com/questions/33513051/structures-and-vectors-in-boost-shared-memory/33518518#33518518) : I assume the following retrieves the data without making a copy : `auto* v = smt.find_or_construct >("InDataVector")(smt.get_segment_manager());` It seems to be what I am looking for. I am quite curious to know if there is any drawback or if the runtime cost of using it is insignificant (I guess I will find out soon anyway if it is a problem). Thanks a lot for your help :D – Norgannon Jun 26 '18 at 11:48
  • Indeed. The profiler is your friend, but in general, there is only overhead in the allocator/offset_ptr "swizzling" (I saw your other question) – sehe Jun 26 '18 at 11:48

0 Answers0