From d89d20dac24403a882173d1e5a2dc753be8c8d39 Mon Sep 17 00:00:00 2001 From: Taras Tsugrii Date: Sun, 27 Aug 2023 19:20:00 -0700 Subject: [PATCH] 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 --- src/unicode.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/unicode.cpp b/src/unicode.cpp index c6100dd0f..6cea8db80 100644 --- a/src/unicode.cpp +++ b/src/unicode.cpp @@ -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