FullscreenUI: Avoid per-frame memory allocations with SmallString

Backport of de1a1af908
This commit is contained in:
Stenzek 2024-04-12 23:49:44 +10:00 committed by Connor McLaughlin
parent 35525d5304
commit a1ec590bf8
5 changed files with 47 additions and 45 deletions

View File

@ -1188,8 +1188,8 @@ void FullscreenUI::DrawInputBindingButton(
TinyString title;
title.format("{}/{}", section, name);
std::string value = bsi->GetStringValue(section, name);
const bool oneline = (std::count_if(value.begin(), value.end(), [](char ch) { return (ch == '&'); }) <= 1);
SmallString value = bsi->GetSmallStringValue(section, name);
const bool oneline = (value.count('&') <= 1);
ImRect bb;
bool visible, hovered, clicked;
@ -1269,7 +1269,7 @@ void FullscreenUI::DrawInputBindingButton(
{
BeginInputBinding(bsi, type, section, name, display_name);
}
else if (ImGui::IsItemClicked(ImGuiMouseButton_Right) || ImGui::IsKeyPressed(ImGuiKey_NavGamepadInput, false))
else if (ImGui::IsItemClicked(ImGuiMouseButton_Right) || ImGui::IsKeyPressed(ImGuiKey_NavGamepadMenu, false))
{
bsi->DeleteValue(section, name);
SetSettingsChanged(bsi);
@ -1497,8 +1497,8 @@ void FullscreenUI::DrawIntRangeSetting(SettingsInterface* bsi, const char* title
const bool game_settings = IsEditingGameSettings(bsi);
const std::optional<int> value =
bsi->GetOptionalIntValue(section, key, game_settings ? std::nullopt : std::optional<int>(default_value));
const std::string value_text(
value.has_value() ? StringUtil::StdStringFromFormat(format, value.value()) : FSUI_STR("Use Global Setting"));
const SmallString value_text =
value.has_value() ? SmallString::from_sprintf(format, value.value()) : SmallString(FSUI_VSTR("Use Global Setting"));
if (MenuButtonWithValue(title, summary, value_text.c_str(), enabled, height, font, summary_font))
ImGui::OpenPopup(title);
@ -1552,8 +1552,8 @@ void FullscreenUI::DrawIntSpinBoxSetting(SettingsInterface* bsi, const char* tit
const bool game_settings = IsEditingGameSettings(bsi);
const std::optional<int> value =
bsi->GetOptionalIntValue(section, key, game_settings ? std::nullopt : std::optional<int>(default_value));
const std::string value_text(
value.has_value() ? StringUtil::StdStringFromFormat(format, value.value()) : FSUI_STR("Use Global Setting"));
const SmallString value_text =
value.has_value() ? SmallString::from_sprintf(format, value.value()) : SmallString(FSUI_VSTR("Use Global Setting"));
static bool manual_input = false;
@ -1673,8 +1673,8 @@ void FullscreenUI::DrawFloatRangeSetting(SettingsInterface* bsi, const char* tit
const bool game_settings = IsEditingGameSettings(bsi);
const std::optional<float> value =
bsi->GetOptionalFloatValue(section, key, game_settings ? std::nullopt : std::optional<float>(default_value));
const std::string value_text(
value.has_value() ? StringUtil::StdStringFromFormat(format, value.value() * multiplier) : FSUI_STR("Use Global Setting"));
const SmallString value_text =
value.has_value() ? SmallString::from_sprintf(format, value.value() * multiplier) : SmallString(FSUI_VSTR("Use Global Setting"));
if (MenuButtonWithValue(title, summary, value_text.c_str(), enabled, height, font, summary_font))
ImGui::OpenPopup(title);
@ -1731,8 +1731,8 @@ void FullscreenUI::DrawFloatSpinBoxSetting(SettingsInterface* bsi, const char* t
const bool game_settings = IsEditingGameSettings(bsi);
const std::optional<float> value =
bsi->GetOptionalFloatValue(section, key, game_settings ? std::nullopt : std::optional<int>(default_value));
const std::string value_text(
value.has_value() ? StringUtil::StdStringFromFormat(format, value.value() * multiplier) : FSUI_STR("Use Global Setting"));
const SmallString value_text =
value.has_value() ? SmallString::from_sprintf(format, value.value() * multiplier) : SmallString(FSUI_VSTR("Use Global Setting"));
static bool manual_input = false;
@ -1864,11 +1864,11 @@ void FullscreenUI::DrawIntRectSetting(SettingsInterface* bsi, const char* title,
bsi->GetOptionalIntValue(section, right_key, game_settings ? std::nullopt : std::optional<int>(default_right));
const std::optional<int> bottom_value =
bsi->GetOptionalIntValue(section, bottom_key, game_settings ? std::nullopt : std::optional<int>(default_bottom));
const std::string value_text(fmt::format(FSUI_FSTR("{0}/{1}/{2}/{3}"),
left_value.has_value() ? StringUtil::StdStringFromFormat(format, left_value.value()) : FSUI_STR("Default"),
top_value.has_value() ? StringUtil::StdStringFromFormat(format, top_value.value()) : FSUI_STR("Default"),
right_value.has_value() ? StringUtil::StdStringFromFormat(format, right_value.value()) : FSUI_STR("Default"),
bottom_value.has_value() ? StringUtil::StdStringFromFormat(format, bottom_value.value()) : FSUI_STR("Default")));
const SmallString value_text = SmallString::from_format(FSUI_FSTR("{0}/{1}/{2}/{3}"),
left_value.has_value() ? TinyString::from_sprintf(format, left_value.value()) : TinyString(FSUI_VSTR("Default")),
top_value.has_value() ? TinyString::from_sprintf(format, top_value.value()) : TinyString(FSUI_VSTR("Default")),
right_value.has_value() ? TinyString::from_sprintf(format, right_value.value()) : TinyString(FSUI_VSTR("Default")),
bottom_value.has_value() ? TinyString::from_sprintf(format, bottom_value.value()) : TinyString(FSUI_VSTR("Default")));
static bool manual_input = false;
@ -2005,8 +2005,8 @@ void FullscreenUI::DrawStringListSetting(SettingsInterface* bsi, const char* tit
bool translate_options, bool enabled, float height, ImFont* font, ImFont* summary_font, const char* translation_ctx)
{
const bool game_settings = IsEditingGameSettings(bsi);
const std::optional<std::string> value(
bsi->GetOptionalStringValue(section, key, game_settings ? std::nullopt : std::optional<const char*>(default_value)));
const std::optional<SmallString> value(
bsi->GetOptionalSmallStringValue(section, key, game_settings ? std::nullopt : std::optional<const char*>(default_value)));
if (option_count == 0)
{
@ -2075,8 +2075,8 @@ void FullscreenUI::DrawStringListSetting(SettingsInterface* bsi, const char* tit
ImFont* summary_font)
{
const bool game_settings = IsEditingGameSettings(bsi);
const std::optional<std::string> value(
bsi->GetOptionalStringValue(section, key, game_settings ? std::nullopt : std::optional<const char*>(default_value)));
const std::optional<SmallString> value(
bsi->GetOptionalSmallStringValue(section, key, game_settings ? std::nullopt : std::optional<const char*>(default_value)));
if (MenuButtonWithValue(
title, summary, value.has_value() ? value->c_str() : FSUI_CSTR("Use Global Setting"), enabled, height, font, summary_font))
@ -2214,8 +2214,8 @@ void FullscreenUI::DrawPathSetting(SettingsInterface* bsi, const char* title, co
ImFont* font /* = g_large_font */, ImFont* summary_font /* = g_medium_font */)
{
const bool game_settings = IsEditingGameSettings(bsi);
const std::optional<std::string> value(
bsi->GetOptionalStringValue(section, key, game_settings ? std::nullopt : std::optional<const char*>(default_value)));
const std::optional<SmallString> value(
bsi->GetOptionalSmallStringValue(section, key, game_settings ? std::nullopt : std::optional<const char*>(default_value)));
if (MenuButton(title, value.has_value() ? value->c_str() : FSUI_CSTR("Use Global Setting")))
{
@ -2653,7 +2653,7 @@ void FullscreenUI::DrawSummarySettingsPage()
if (s_game_settings_entry->type == GameList::EntryType::ELF)
{
const std::string iso_path(bsi->GetStringValue("EmuCore", "DiscPath"));
const SmallString iso_path = bsi->GetSmallStringValue("EmuCore", "DiscPath");
if (MenuButton(FSUI_ICONSTR(ICON_FA_COMPACT_DISC, "Disc Path"), iso_path.empty() ? "No Disc" : iso_path.c_str()))
{
auto callback = [](const std::string& path) {
@ -2804,7 +2804,7 @@ void FullscreenUI::DrawBIOSSettingsPage()
DrawFolderSetting(bsi, FSUI_ICONSTR(ICON_FA_FOLDER_OPEN, "Change Search Directory"), "Folders", "Bios", EmuFolders::Bios);
const std::string bios_selection(GetEditingSettingsInterface()->GetStringValue("Filenames", "BIOS", ""));
const SmallString bios_selection = GetEditingSettingsInterface()->GetSmallStringValue("Filenames", "BIOS", "");
if (MenuButtonWithValue(FSUI_ICONSTR(ICON_PF_MICROCHIP, "BIOS Selection"),
FSUI_CSTR("Changes the BIOS image used to start future sessions."),
bios_selection.empty() ? FSUI_CSTR("Automatic") : bios_selection.c_str()))
@ -3661,8 +3661,8 @@ void FullscreenUI::DrawMemoryCardSettingsPage()
const bool enabled = GetEffectiveBoolSetting(bsi, "MemoryCards", enable_key.c_str(), true);
std::optional<std::string> value(bsi->GetOptionalStringValue("MemoryCards", file_key.c_str(),
IsEditingGameSettings(bsi) ? std::nullopt : std::optional<const char*>(FileMcd_GetDefaultName(port).c_str())));
const std::optional<SmallString> value = bsi->GetOptionalSmallStringValue("MemoryCards", file_key.c_str(),
IsEditingGameSettings(bsi) ? std::nullopt : std::optional<const char*>(FileMcd_GetDefaultName(port).c_str()));
if (MenuButtonWithValue(SmallString::from_format(fmt::runtime(FSUI_ICONSTR_S(ICON_FA_FILE, "Card Name", "##card_name_{}")), port),
FSUI_CSTR("The selected memory card image will be used for this slot."),
@ -3675,7 +3675,7 @@ void FullscreenUI::DrawMemoryCardSettingsPage()
if (value.has_value() && !value->empty())
{
options.emplace_back(fmt::format(FSUI_FSTR("{} (Current)"), value.value()), true);
names.push_back(std::move(value.value()));
names.emplace_back(value->view());
}
for (AvailableMcdInfo& mci : FileMcd_GetAvailableCards(IsEditingGameSettings(bsi)))
{
@ -4020,7 +4020,7 @@ void FullscreenUI::DrawControllerSettingsPage()
DrawInputBindingButton(
bsi, InputBindingInfo::Type::Macro, section, TinyString::from_format("Macro{}", macro_index + 1), "Trigger", nullptr);
std::string binds_string(bsi->GetStringValue(section, fmt::format("Macro{}Binds", macro_index + 1).c_str()));
SmallString binds_string = bsi->GetSmallStringValue(section, fmt::format("Macro{}Binds", macro_index + 1).c_str());
TinyString pretty_binds_string;
if (!binds_string.empty())
{
@ -5198,7 +5198,7 @@ void FullscreenUI::DrawSaveStateSelector(bool is_loading)
break;
}
if (hovered && (ImGui::IsItemClicked(ImGuiMouseButton_Right) || ImGui::IsKeyPressed(ImGuiKey_NavGamepadInput, false)))
if (hovered && (ImGui::IsItemClicked(ImGuiMouseButton_Right) || ImGui::IsKeyPressed(ImGuiKey_NavGamepadMenu, false)))
s_save_state_selector_submenu_index = static_cast<s32>(i);
}
@ -5594,7 +5594,7 @@ void FullscreenUI::DrawGameList(const ImVec2& heading_size)
selected_entry = entry;
if (selected_entry &&
(ImGui::IsItemClicked(ImGuiMouseButton_Right) || ImGui::IsKeyPressed(ImGuiKey_NavGamepadInput, false)))
(ImGui::IsItemClicked(ImGuiMouseButton_Right) || ImGui::IsKeyPressed(ImGuiKey_NavGamepadMenu, false)))
{
HandleGameListOptions(selected_entry);
}
@ -5796,7 +5796,7 @@ void FullscreenUI::DrawGameGrid(const ImVec2& heading_size)
if (pressed)
HandleGameListActivate(entry);
if (hovered && (ImGui::IsItemClicked(ImGuiMouseButton_Right) || ImGui::IsKeyPressed(ImGuiKey_NavGamepadInput, false)))
if (hovered && (ImGui::IsItemClicked(ImGuiMouseButton_Right) || ImGui::IsKeyPressed(ImGuiKey_NavGamepadMenu, false)))
HandleGameListOptions(entry);
}
@ -6318,11 +6318,11 @@ void FullscreenUI::DrawAchievementsSettingsPage(std::unique_lock<std::mutex>& se
{
ImGui::PushStyleColor(ImGuiCol_TextDisabled, ImGui::GetStyle().Colors[ImGuiCol_Text]);
ActiveButton(SmallString::from_format(
fmt::runtime(FSUI_ICONSTR(ICON_FA_USER, "Username: {}")), bsi->GetStringValue("Achievements", "Username")),
fmt::runtime(FSUI_ICONSTR(ICON_FA_USER, "Username: {}")), bsi->GetTinyStringValue("Achievements", "Username")),
false, false, ImGuiFullscreen::LAYOUT_MENU_BUTTON_HEIGHT_NO_SUMMARY);
ActiveButton(SmallString::from_format(fmt::runtime(FSUI_ICONSTR(ICON_FA_CLOCK, "Login token generated on {}")),
TimeToPrintableString(static_cast<time_t>(
StringUtil::FromChars<u64>(bsi->GetStringValue("Achievements", "LoginTimestamp", "0")).value_or(0)))),
StringUtil::FromChars<u64>(bsi->GetTinyStringValue("Achievements", "LoginTimestamp", "0")).value_or(0)))),
false, false, ImGuiFullscreen::LAYOUT_MENU_BUTTON_HEIGHT_NO_SUMMARY);
ImGui::PopStyleColor();

View File

@ -1326,7 +1326,7 @@ bool ImGuiFullscreen::RangeButton(const char* title, const char* summary, s32* v
if (!visible)
return false;
const std::string value_text(StringUtil::StdStringFromFormat(format, *value));
const SmallString value_text = SmallString::from_sprintf(format, *value);
const ImVec2 value_size(ImGui::CalcTextSize(value_text.c_str()));
const float midpoint = bb.Min.y + font->FontSize + LayoutScale(4.0f);
@ -1396,7 +1396,7 @@ bool ImGuiFullscreen::RangeButton(const char* title, const char* summary, float*
if (!visible)
return false;
const std::string value_text(StringUtil::StdStringFromFormat(format, *value));
const SmallString value_text = SmallString::from_sprintf(format, *value);
const ImVec2 value_size(ImGui::CalcTextSize(value_text.c_str()));
const float midpoint = bb.Min.y + font->FontSize + LayoutScale(4.0f);

View File

@ -805,18 +805,18 @@ void SaveStateSelectorUI::DestroyTextures()
void SaveStateSelectorUI::RefreshHotkeyLegend()
{
auto format_legend_entry = [](std::string binding, std::string_view caption) {
auto format_legend_entry = [](SmallString binding, std::string_view caption) {
InputManager::PrettifyInputBinding(binding);
return fmt::format("{} - {}", binding, caption);
};
s_load_legend = format_legend_entry(Host::GetStringSettingValue("Hotkeys", "LoadStateFromSlot"),
s_load_legend = format_legend_entry(Host::GetSmallStringSettingValue("Hotkeys", "LoadStateFromSlot"),
TRANSLATE_STR("ImGuiOverlays", "Load"));
s_save_legend = format_legend_entry(Host::GetStringSettingValue("Hotkeys", "SaveStateToSlot"),
s_save_legend = format_legend_entry(Host::GetSmallStringSettingValue("Hotkeys", "SaveStateToSlot"),
TRANSLATE_STR("ImGuiOverlays", "Save"));
s_prev_legend = format_legend_entry(Host::GetStringSettingValue("Hotkeys", "PreviousSaveStateSlot"),
s_prev_legend = format_legend_entry(Host::GetSmallStringSettingValue("Hotkeys", "PreviousSaveStateSlot"),
TRANSLATE_STR("ImGuiOverlays", "Select Previous"));
s_next_legend = format_legend_entry(Host::GetStringSettingValue("Hotkeys", "NextSaveStateSlot"),
s_next_legend = format_legend_entry(Host::GetSmallStringSettingValue("Hotkeys", "NextSaveStateSlot"),
TRANSLATE_STR("ImGuiOverlays", "Select Next"));
}

View File

@ -349,12 +349,12 @@ std::string InputManager::ConvertInputBindingKeysToString(InputBindingInfo::Type
return ss.str();
}
bool InputManager::PrettifyInputBinding(std::string& binding)
bool InputManager::PrettifyInputBinding(SmallStringBase& binding)
{
if (binding.empty())
return false;
const std::string_view binding_view(binding);
const std::string_view binding_view = binding.view();
SmallString ret;
bool changed = false;
@ -387,7 +387,7 @@ bool InputManager::PrettifyInputBinding(std::string& binding)
}
if (changed)
binding = ret.view();
binding = ret;
return changed;
}

View File

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2002-2023 PCSX2 Dev Team
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
// SPDX-License-Identifier: LGPL-3.0+
#pragma once
@ -16,6 +16,8 @@
#include "pcsx2/Config.h"
class SmallStringBase;
/// Class, or source of an input event.
enum class InputSourceType : u32
{
@ -207,7 +209,7 @@ namespace InputManager
std::string ConvertInputBindingKeysToString(InputBindingInfo::Type binding_type, const InputBindingKey* keys, size_t num_keys);
/// Represents a binding with icon fonts, if available.
bool PrettifyInputBinding(std::string& binding);
bool PrettifyInputBinding(SmallStringBase& binding);
/// Returns a list of all hotkeys.
std::vector<const HotkeyInfo*> GetHotkeyList();