Skip to content

Commit

Permalink
Avoid potentially unnecessary string reallocs.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ttsugriy authored and anonrig committed Aug 28, 2023
1 parent a9c1c4d commit 24d3812
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ std::string percent_decode(const std::string_view input, size_t first_percent) {
if (first_percent == std::string_view::npos) {
return std::string(input);
}
std::string dest(input.substr(0, first_percent));
std::string dest;
dest.reserve(input.length());
dest.append(input.substr(0, first_percent));
const char* pointer = input.data() + first_percent;
const char* end = input.data() + input.size();
// Optimization opportunity: if the following code gets
Expand Down

0 comments on commit 24d3812

Please sign in to comment.