Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:23:48+00:00 2026-05-23T05:23:48+00:00

My question is the same as discussed in this thread from five years ago

  • 0

My question is the same as discussed in this thread from five years ago (which has no good answer).

I’m serializing my objects into a byte buffer, like so:

std::string serial_str;
for (i = 1; i < 10000; i++)
{
    boost::iostreams::back_insert_device<std::string> inserter(serial_str);
    boost::iostreams::stream<boost::iostreams::back_insert_device<std::string> > s(inserter);
    boost::archive::binary_oarchive oa(s);

    oa << obj;

    s.flush();

    // code to send serial_str's content to another process, omitted.

    serial_str.clear(); // clear the buffer so it can be reused to serialize the next object
}    

When I do this in a loop, the performance is quite bad: I get ~14,000 objects / sec.

I’ve pinpointed the problem down to the recreation of the binary_oarchive. If I just write into the same string with the same archive instance in a loop, I get ~220,000 objects/sec, but then, the objects are serialized one after the other sequentially, which isn’t what I want: I want to clear and reuse the same buffer (seek to its beginning) after each object is serialized.

How can I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:23:49+00:00Added an answer on May 23, 2026 at 5:23 am

    Yes, you absolutely can reuse it, in a sense. The oarchive simply wraps up a stream and doesn’t know what’s going on with the stream’s data, so the trick is to implement your own stream (which isn’t fun) to allow you to “reset” the actual underlaying data stream. I’ve written something like this before and it works wonderfully.

    Some gotchas to be aware of though:

    The oarchive won’t keep writing out header information (since if it persists it’s treating everything as one big stream), so you’ll want to disable the headers:

    boost::archive::binary_oarchive oa(s, boost::archive::no_codecvt | boost::archive::no_header);
    

    Also, because you’re reusing an oarchive, you have to be extremely careful about managing its internal type table. If all you’re serializing are ints, floats, etc, then you’ll be fine, but as soon as you start serializing classes, strings, and the like you can’t rely on the default type enumeration that the archive uses when reusing the archive like this. The Boost documentation doesn’t really get into this, but for anything complex, you need to do the following for every type the archive will come across:

    oa.template register_type<std::string>();
    oa.template register_type<MyClass>();
    oa.template register_type<std::shared_ptr<MyClass> >();
    

    And so on.. for all your types, all std::vectors of them, all std::shared_ptrs of them, etc. This is vital. Otherwise you’ll only be able to read back your streams if you use a shared iarchive and read them in the exact same order they were serialized out.

    The consequence is that your iarchive needs to register all the types in the exact same way and order as their oarchive (I wrote some handy helpers using mpl to do help me with this).

    Serializing back in through an iarchive can also share the same iarchive, however all the same conditions apply:

    • You need to write your own stream (so it can be redirected/reset)
    • Disable the archive headers
    • Have the register types

    So yes, reusing an oarchive/iarchive is possible, but it’s a bit of a pain. Once you’ve got it sorted out though, it’s pretty awesome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this same question for VIM and it has been something that I
I had the same question as was asked in this thread , i.e. I
First of all, I am aware that this question has been discussed many times
I thought I would rewrite this question (same iteration). The original was how to
This is the same question as this but I'm looking for a classic ASP
This is the same question for older version of Scala, but they say that
I know this topic has been discussed to death, but there is one thing
This question has no practical issues associated with it, it is more a matter
OK, this must be a duplicate question, but I cannot find the answer: I
Ok so in another question something was being discussed, and this link was mentioned:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.