From d663dd1cc9b518aef3f6fac693f74696a5f648cb Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 14 Jan 2024 13:51:58 -0700 Subject: [PATCH] Reduce string allocations --- examples/joystick/Joystick.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/joystick/Joystick.cpp b/examples/joystick/Joystick.cpp index bca3396d8..05d997681 100644 --- a/examples/joystick/Joystick.cpp +++ b/examples/joystick/Joystick.cpp @@ -18,17 +18,17 @@ struct JoystickObject sf::Text value; }; -using Texts = std::unordered_map; +using Texts = std::unordered_map; Texts texts; std::ostringstream sstr; float threshold = 0.1f; // Axes labels in as strings -const std::array axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"}; +const std::array axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"}; // Helper to set text entries to a specified value template -void set(const std::string& label, const T& value) +void set(std::string_view label, const T& value) { sstr.str(""); sstr << value; @@ -122,7 +122,8 @@ int main() for (unsigned int i = 0; i < sf::Joystick::AxisCount; ++i) { const auto [it, success] = texts.try_emplace(axislabels[i], - JoystickObject{{font, axislabels[i] + ":"}, {font, "N/A"}}); + JoystickObject{{font, std::string(axislabels[i]) + ":"}, + {font, "N/A"}}); auto& [label, value] = it->second; label.setPosition({5.f, 5.f + (static_cast(i + 4) * font.getLineSpacing(14))}); value.setPosition({80.f, 5.f + (static_cast(i + 4) * font.getLineSpacing(14))});