简体   繁体   中英

How can I substitute a binary stream for a string stream, to avoid casting?

I'm currently investigating a few methods to transfer a complex CGAL object instance to a different process. In another post, I've asked about custom allocators, but another method is string streams. (FYI shared memory is not an option)

The current CGAL::Nef_polyhedron_3 provides iostreaming:

// In main process
Nef_polyhedron_3 NP3;
stringstream ss;
ss << NP3;
sendToOtherProcess(ss.str());

// In separate process
stringstream ss(stringFromOtherProcess);
Nef_polyhedron_3 NP3;
ss >> NP3;

Now, the output streaming is slow, and the input streaming is four times slower.

The reason being (I am guessing) is all the casting. The class which does it can be found here: https://github.ugent.be/divhaere/cgal/blob/master/include/CGAL/Nef_3/SNC_io_parser.h

To take just a snippet of one of the members of the class (ln 1481):

  in >> hx >> hy >> hz >> hw;
  vh->point() = Point_3(hx,hy,hz,hw);

Doing that for many thousands of points is taking a while. On the output side it's faster (not sure why exactly)

Is there any way I can provide some kind of binary stream, that won't do all the casting? I'm aware one can write to any stream in binary with read(), write(), but I can't (easily) change the CGAL SNC_io_parser.h. I guess ideally I'd like to be able to provide a stream that when asked to stream (either way), it did it in binary.

Any help gratefully received.

Marcos

The reason being (I am guessing) is all the casting... Doing that for many thousands of points is taking a while. On the output side it's faster (not sure why exactly)

You may like to run it under profiler to be able to tell with certainty where the time is being spend.

Is there any way I can provide some kind of binary stream, that won't do all the casting?

You are using std::ostream with operator<<() and std::istream with operator>>() interfaces which serialize arithmetic types into and from text and this behaviour can not be changed.

You can replicate the serialization functions of Nef_polyhedron_3 and make them accept some other stream types that serialize into/from binary formats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM