This question starts with a bit of code, just because I think it is easier to see what I am after:
/*static*/
void
Url::Split
(std::list<std::string> & url
, const std::string& stringUrl
)
{
std::string collector;
collector.reserve(stringUrl.length());
for (auto c : stringUrl)
{
if (PathSeparator == c)
{
url.push_back(collector);
collector.clear(); // Sabotages my optimization with reserve() above!
}
else
{
collector.push_back(c);
}
}
url.push_back(collector);
}
In the code above, the collector.reserve(stringUrl.length());
line is supposed to reduce the amount of heap operations performed during the loop below. Each substring cannot be longer than the whole url, after all and so reserving enough capacity as I do it looks like a good idea.
But, once a substring is finished and I add it to the url parts list, I need to reset the string to length 0 one way or another. Brief "peek definition" inspection suggests to me that at least on my platform, the reserved buffer will be released and with that, the purpose of my reserve() call is compromised.
Internally it calls some _Eos(0)
in case of clear.
I could as well accomplish the same with collector.resize(0)
but peeking definition reveals it also calls _Eos(newsize)
internally, so the behavior is the same as in case of calling clear()
.
Now the question is, if there is a portable way to establish the intended optimization and which std::string
function would help me with that.
Of course I could write collector[0] = '\0';
but that looks very off to me.
Side note: While I found similar questions, I do not think this is a duplicate of any of them.
Thanks, in advance.
Aucun commentaire:
Enregistrer un commentaire