From e7bedb15ef9da75141bf233434c92c2960a1d1e3 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sun, 3 Mar 2024 17:23:54 +0100 Subject: [PATCH] Remove some extra allocations. --- src/video/font.cpp | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/video/font.cpp b/src/video/font.cpp index 74b3a08219..62ff60665a 100644 --- a/src/video/font.cpp +++ b/src/video/font.cpp @@ -48,10 +48,10 @@ -- Variables ----------------------------------------------------------------------------*/ -using FontMap = std::map, std::less<>>; +using FontMap = std::map>; static FontMap Fonts; /// Font mappings -using FontColorMap = std::map, std::less<>>; +using FontColorMap = std::map>; static FontColorMap FontColors; /// Map of ident to font color. static const CFontColor *LastTextColor; /// Last text color @@ -913,7 +913,7 @@ void CFont::DynamicLoad() const void LoadFonts() { for (auto &[key, font] : Fonts) { - font->Load(); + font.Load(); } // TODO: remove this @@ -931,16 +931,15 @@ void LoadFonts() */ /* static */ CFont *CFont::New(const std::string &ident, CGraphic *g) { - auto &font = Fonts[ident]; - if (font) { - if (font->G != g) { - CGraphic::Free(font->G); + auto &[it, inserted] = Fonts.insert({ident, CFont(ident)}); + auto &[key, font] = *it; + if (!inserted) { + if (font.G != g) { + CGraphic::Free(font.G); } - } else { - font.reset(new CFont(ident)); } - font->G = g; - return font.get(); + font.G = g; + return &font; } /** @@ -967,12 +966,7 @@ void LoadFonts() ErrorPrint("font not found: '%s'\n", ident.data()); return nullptr; } - auto &font = it->second; - if (font == nullptr) { - ErrorPrint("font not found: '%s'\n", ident.data()); - return nullptr; - } - return font.get(); + return &it->second; } /** @@ -984,12 +978,8 @@ void LoadFonts() */ /* static */ CFontColor *CFontColor::New(const std::string &ident) { - auto &fc = FontColors[ident]; - - if (fc == nullptr) { - fc = std::make_unique(ident); - } - return fc.get(); + auto [it, inserted] = FontColors.emplace(ident, ident); + return &it->second; } /** @@ -1006,7 +996,7 @@ void LoadFonts() ErrorPrint("font color not found: '%s'\n", ident.data()); ExitFatal(-1); } - return it->second.get(); + return &it->second; } /**