This is my current function that de-serializes data received via Boost:Asio UDP transmission. It works perfectly, however the performance is pretty bad. About 4000 or so calls per second will use ~16% of CPU, which is a full thread of an I7.
Running a performance test on the code shows that this line uses >95% of the cpu time:
text_iarchive LLArchive(LLStream);
My question is simple: is there a way I can re-use a text_iarchive without having to create a new one each time the function is called? (a similar thing is possible in C# with memorystreams and other variables needed to deserialise data). I've searched through the Boost documentation and no mention was made of anything like it.
What I essentially want is to put the function bellow in a class and have as many variables as possible defined as members that would simply be used inside the function through re-initialization (clearing buffer/stream, re-setting data etc). Will this even improve the performance? Would changing the stream passed into the archive be enough to do the trick (does it bind it somewhere so that if we change the passed stream, the one the archive sets to itself changes as well) ?
Is it possible?
Thank you very much for your time!
Full function code:
using namespace boost::archive;
using namespace boost::iostreams;
Packet InboundStreamToInternalPacket(boost::array<char, 5000> inboundStream)
{
Packet receivedPacket;
basic_array_source<char> arraySourceLL(inboundStream.data(), inboundStream.size());
stream<basic_array_source<char>> LLStream(arraySourceLL);
text_iarchive LLArchive(LLStream);
LLArchive >> receivedPacket;
return receivedPacket;
}
Edit 1:
Tried closing and opening the stream again as if a new source was added, crashes with "boost::archive::archive_exception at memory location xxxxxx" when de-serializing into the second Packet.
Packet InboundStreamToInternalPacket(boost::array<char, 5000> inboundStream)
{
Packet receivedPacket;
Packet receivedPacket2;
basic_array_source<char> arraySourceLL(inboundStream.data(), inboundStream.size());
stream<basic_array_source<char>> LLStream;
LLStream.open(arraySourceLL);
text_iarchive LLArchive(LLStream);
LLArchive >> receivedPacket;
LLStream.close();
LLStream.open(arraySourceLL);
LLArchive >> receivedPacket2;
return receivedPacket;
}