Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I preallocate std::stringstream?

I use std::stringstream extensively to construct strings and error messages in my application. The stringstreams are usually very short life automatic variables.

Will such usage cause heap reallocation for every variable? Should I switch from temporary to class-member stringstream variable?

In latter case, how can I reserve stringstream buffer? (Should I initialize it with a large enough string or is there a more elegant method?)

like image 715
jackhab Avatar asked Dec 21 '09 16:12

jackhab


People also ask

Is Stringstream deprecated?

strstream has been deprecated since C++98, std::stringstream and boost::iostreams::array are the recommended replacements.

What should I use for Stringstream?

A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input.

Does Stringstream allocate memory?

stringstream is constructed with dummy. This copies the entire string's contents into an internal buffer, which is preallocated. dummy is then cleared and then erased, freeing up its allocation.

Why do we need Stringstream?

Stringstream class is used for insertion and extraction of data to/from the string objects. It acts as a stream for the string object. The stringstream class is similar to cin and cout streams except that it doesn't have an input-output channel.


2 Answers

Have you profiled your execution, and found them to be a source of slow down?

Consider their usage. Are they mostly for error messages outside the normal flow of your code?

As far as reserving space...

Some implementations probably reserve a small buffer before any allocation takes place for the stringstream. Many implementations of std::string do this.

Another option might be (untested!)

std::string str; str.reserve(50); std::stringstream sstr(str); 

You might find some more ideas in this gamedev thread.

edit:

Mucking around with the stringstream's rdbuf might also be a solution. This approach is probably Very Easy To Get Wrong though, so please be sure it's absolutely necessary. Definitely not elegant or concise.

like image 54
luke Avatar answered Oct 11 '22 02:10

luke


Although "mucking around with the stringstream's rdbuf...is probably Very Easy To Get Wrong", I went ahead and hacked together a proof-of-concept anyway for fun, as it has always bugged me that there is no easy way to reserve storage for stringstream. Again, as @luke said, you are probably better off optimizing what your profiler tells you needs optimizing, so this is just to address "What if I want to do it anyway?".

Instead of mucking around with stringstream's rdbuf, I made my own, which does pretty much the same thing. It implements only the minimum, and uses a string as a buffer. Don't ask me why I called it a VECTOR_output_stream. This is just a quickly-hacked-together thing.

constexpr auto preallocated_size = 256; auto stream = vector_output_stream(preallocated_size); stream << "My parrot ate " << 3 << " cookies."; cout << stream.str() << endl; 
like image 27
Mona the Monad Avatar answered Oct 11 '22 04:10

Mona the Monad