Should I preallocate std::stringstream?

南笙酒味 提交于 2019-11-30 07:51:29

问题


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?)


回答1:


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.




回答2:


I'm not sure, but I suspect that stringbuf of stringstream is tightly related with resulted string. So I suspect that you can use ss.seekp(reserved-1); ss.put('\0'); to reserve reserved amount of bytes inside of underlying string of ss. Actually I'd like to see something like ss.seekp(reserved); ss.trunc();, but there is no trunc() method for streams.




回答3:


The Bad

This is an old question, but even as of C++1z/C++2a in Visual Studio 2019, stringstream has no ideal way of reserving a buffer.

The other answers to this question do not work at all and for the following reasons:

  • calling reserve on an empty string yields an empty string, so stringstream constructor doesn't need to allocate to copy the contents of that string.

  • seekp on a stringstream still seems to be undefined behavior and/or does nothing.

The Good

This code segment works as expected, with ss being preallocated with the requested size.

std::string dummy(reserve, '\0');
std::stringstream ss(dummy);
dummy.clear();
dummy.shrink_to_fit();

The code can also be written as a one-liner std::stringstream ss(std::string(reserve, '\0'));.

The Ugly

What really happens in this code segment is the following:

  • dummy is preallocated with the reserve, and the buffer is subsequently filled with null bytes (required for the constructor).
  • 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.

This means that in order to preallocate a stringstream, two allocations, one fill, and one copy takes place. The worst part is that during the expression, twice as much memory is needed for the desired allocation. Yikes!

For most use cases, this might not matter at all and it's OK to take the extra fill and copy hit to have fewer reallocations.



来源:https://stackoverflow.com/questions/1941064/should-i-preallocate-stdstringstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!