Skip to content

Commit 24d3812

Browse files
ttsugriyanonrig
authored andcommitted
Avoid potentially unnecessary string reallocs.
In case `dest` is not created using a subset of the `input` `dest.reserve(input.size())` would result in increasing `dest`s size with likely reallocation. On top of being less efficient new version also generates significantly less asm with GCC 13. https://compiler-explorer.com/z/x3avEdWGM
1 parent a9c1c4d commit 24d3812

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/unicode.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,9 @@ std::string percent_decode(const std::string_view input, size_t first_percent) {
383383
if (first_percent == std::string_view::npos) {
384384
return std::string(input);
385385
}
386-
std::string dest(input.substr(0, first_percent));
386+
std::string dest;
387387
dest.reserve(input.length());
388+
dest.append(input.substr(0, first_percent));
388389
const char* pointer = input.data() + first_percent;
389390
const char* end = input.data() + input.size();
390391
// Optimization opportunity: if the following code gets

0 commit comments

Comments
 (0)