From 9125a7ce6837ce7da9ad7967c20567f67fd23d8d Mon Sep 17 00:00:00 2001 From: Zach Nelson Date: Wed, 18 Feb 2026 04:55:41 -0600 Subject: [PATCH] perf: Avoid redundant font map lookups (#933) ## Summary **What is the goal of this PR?** Several methods in GfxRenderer were doing a `count()` followed by `at()` on the fonts map, effectively doing the same map lookup unnecessarily. This can be avoided by doing a single `find()` and reusing the iterator. --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**NO**_ --- lib/GfxRenderer/GfxRenderer.cpp | 42 ++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/GfxRenderer/GfxRenderer.cpp b/lib/GfxRenderer/GfxRenderer.cpp index 32afb7a8..5ac49fd5 100644 --- a/lib/GfxRenderer/GfxRenderer.cpp +++ b/lib/GfxRenderer/GfxRenderer.cpp @@ -74,13 +74,14 @@ void GfxRenderer::drawPixel(const int x, const int y, const bool state) const { } int GfxRenderer::getTextWidth(const int fontId, const char* text, const EpdFontFamily::Style style) const { - if (fontMap.count(fontId) == 0) { + const auto fontIt = fontMap.find(fontId); + if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return 0; } int w = 0, h = 0; - fontMap.at(fontId).getTextDimensions(text, &w, &h, style); + fontIt->second.getTextDimensions(text, &w, &h, style); return w; } @@ -100,11 +101,12 @@ void GfxRenderer::drawText(const int fontId, const int x, const int y, const cha return; } - if (fontMap.count(fontId) == 0) { + const auto fontIt = fontMap.find(fontId); + if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return; } - const auto font = fontMap.at(fontId); + const auto& font = fontIt->second; // no printable characters if (!font.hasPrintableChars(text, style)) { @@ -709,52 +711,58 @@ int GfxRenderer::getScreenHeight() const { } int GfxRenderer::getSpaceWidth(const int fontId) const { - if (fontMap.count(fontId) == 0) { + const auto fontIt = fontMap.find(fontId); + if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return 0; } - return fontMap.at(fontId).getGlyph(' ', EpdFontFamily::REGULAR)->advanceX; + return fontIt->second.getGlyph(' ', EpdFontFamily::REGULAR)->advanceX; } int GfxRenderer::getTextAdvanceX(const int fontId, const char* text) const { - if (fontMap.count(fontId) == 0) { + const auto fontIt = fontMap.find(fontId); + if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return 0; } uint32_t cp; int width = 0; + const auto& font = fontIt->second; while ((cp = utf8NextCodepoint(reinterpret_cast(&text)))) { - width += fontMap.at(fontId).getGlyph(cp, EpdFontFamily::REGULAR)->advanceX; + width += font.getGlyph(cp, EpdFontFamily::REGULAR)->advanceX; } return width; } int GfxRenderer::getFontAscenderSize(const int fontId) const { - if (fontMap.count(fontId) == 0) { + const auto fontIt = fontMap.find(fontId); + if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return 0; } - return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->ascender; + return fontIt->second.getData(EpdFontFamily::REGULAR)->ascender; } int GfxRenderer::getLineHeight(const int fontId) const { - if (fontMap.count(fontId) == 0) { + const auto fontIt = fontMap.find(fontId); + if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return 0; } - return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->advanceY; + return fontIt->second.getData(EpdFontFamily::REGULAR)->advanceY; } int GfxRenderer::getTextHeight(const int fontId) const { - if (fontMap.count(fontId) == 0) { + const auto fontIt = fontMap.find(fontId); + if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return 0; } - return fontMap.at(fontId).getData(EpdFontFamily::REGULAR)->ascender; + return fontIt->second.getData(EpdFontFamily::REGULAR)->ascender; } void GfxRenderer::drawTextRotated90CW(const int fontId, const int x, const int y, const char* text, const bool black, @@ -764,11 +772,13 @@ void GfxRenderer::drawTextRotated90CW(const int fontId, const int x, const int y return; } - if (fontMap.count(fontId) == 0) { + const auto fontIt = fontMap.find(fontId); + if (fontIt == fontMap.end()) { LOG_ERR("GFX", "Font %d not found", fontId); return; } - const auto font = fontMap.at(fontId); + + const auto& font = fontIt->second; // No printable characters if (!font.hasPrintableChars(text, style)) {