From 4662e06b350d09887f37683345db3d6d7746edc8 Mon Sep 17 00:00:00 2001 From: shenlei Date: Mon, 12 Oct 2026 16:16:48 +0900 Subject: [PATCH] Cut per-frame text tail and label allocations - Font: walk UTF-8 codepoints in place instead of building a vector; reuse the texture-cache key buffer on the hit path. - Text tails: show/ShowAll test isShowing (kept equal to list membership) instead of a linear find per tail, which made the per-frame ShowAllTextTail O(N^2); memoize label widths measured by both Arrange and Render. - Tail painter reuses one CGraphicTextInstance and font name. - CGraphicTextInstance::Render no longer copies the text unless secret. Co-Authored-By: Claude Opus 5.5 --- src/eter_render/TextTailManager.cpp | 8 ++++---- src/eter_render/TextTailManager.h | 11 +++++++++-- src/eter_ui/Font.cpp | 25 +++++++++++++++---------- src/eter_ui/PythonWindow.cpp | 7 ++++--- src/scripting/python_native_modules.cpp | 13 +++++++++---- 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/eter_render/TextTailManager.cpp b/src/eter_render/TextTailManager.cpp index c9c27bad..f2accb92 100644 --- a/src/eter_render/TextTailManager.cpp +++ b/src/eter_render/TextTailManager.cpp @@ -132,7 +132,7 @@ void TextTailManager::ShowCharacterTextTail(uint32_t vid) { if (it == m_characterMap.end()) return; auto tail = it->second; - if (std::find(m_characterList.begin(), m_characterList.end(), tail) == m_characterList.end()) { + if (!tail->isShowing) { // isShowing <=> in m_characterList tail->isShowing = true; m_characterList.push_back(tail); } @@ -200,7 +200,7 @@ void TextTailManager::ShowItemTextTail(uint32_t vid) { if (it == m_itemMap.end()) return; auto tail = it->second; - if (std::find(m_itemList.begin(), m_itemList.end(), tail) == m_itemList.end()) { + if (!tail->isShowing) { // isShowing <=> in m_itemList tail->isShowing = true; m_itemList.push_back(tail); } @@ -237,13 +237,13 @@ bool TextTailManager::IsChatTextTail(uint32_t vid) const { void TextTailManager::ShowAllTextTail() { for (auto& [vid, tail] : m_characterMap) { - if (std::find(m_characterList.begin(), m_characterList.end(), tail) == m_characterList.end()) { + if (!tail->isShowing) { // isShowing <=> in m_characterList tail->isShowing = true; m_characterList.push_back(tail); } } for (auto& [vid, tail] : m_itemMap) { - if (std::find(m_itemList.begin(), m_itemList.end(), tail) == m_itemList.end()) { + if (!tail->isShowing) { // isShowing <=> in m_itemList tail->isShowing = true; m_itemList.push_back(tail); } diff --git a/src/eter_render/TextTailManager.h b/src/eter_render/TextTailManager.h index ce095cb4..9024dea8 100644 --- a/src/eter_render/TextTailManager.h +++ b/src/eter_render/TextTailManager.h @@ -124,7 +124,7 @@ public: void Render(SDL_Renderer* renderer, const Gr2SceneRenderer* sceneRenderer = nullptr); using TextPainter = std::function; using TextMeasure = std::function; - void SetTextPainter(TextPainter painter, TextMeasure measure) { m_textPainter = std::move(painter); m_textMeasure = std::move(measure); } + void SetTextPainter(TextPainter painter, TextMeasure measure) { m_textPainter = std::move(painter); m_textMeasure = std::move(measure); m_widths.clear(); } // Queries & Getters size_t GetCharacterCount() const { return m_characterMap.size(); } @@ -148,9 +148,16 @@ public: private: TextPainter m_textPainter; TextMeasure m_textMeasure; + // Names are measured by Arrange and again by Render every frame; memoize per scale. float TextWidth(const std::string& text, float scale = 1.0f) const { - return m_textMeasure ? m_textMeasure(text, scale) : float(text.size()) * 8.0f * scale; + if (!m_textMeasure) return float(text.size()) * 8.0f * scale; + auto& widths = m_widths[scale]; + auto it = widths.find(text); + if (it != widths.end()) return it->second; + if (widths.size() >= 4096) widths.clear(); + return widths.emplace(text, m_textMeasure(text, scale)).first->second; } + mutable std::unordered_map> m_widths; bool isIn(const TextTailItem& src, const TextTailItem& tgt) const; void RenderTextTailBox(SDL_Renderer* renderer, const TextTailItem& tail); void RenderText(SDL_Renderer* renderer, float x, float y, const std::string& text, uint32_t color, float scale = 1.0f, bool shadow = true); diff --git a/src/eter_ui/Font.cpp b/src/eter_ui/Font.cpp index b88b1a87..cf91aee1 100644 --- a/src/eter_ui/Font.cpp +++ b/src/eter_ui/Font.cpp @@ -1,6 +1,7 @@ #include "Font.h" #include #include +#include #include #include #include @@ -13,8 +14,9 @@ namespace UI { namespace { -std::vector Codepoints(const std::string& text) { - std::vector out; +// Visits codepoints in place; measuring/drawing runs per label per frame, so no vector. +template +void ForEachCodepoint(const std::string& text, Visit&& visit) { for (size_t i = 0; i < text.size();) { unsigned c = static_cast(text[i++]); int n = c >= 0xf0 && c <= 0xf4 ? 3 : c >= 0xe0 && c < 0xf0 ? 2 : c >= 0xc2 && c < 0xe0 ? 1 : 0; @@ -24,9 +26,8 @@ std::vector Codepoints(const std::string& text) { value = (value << 6) | (static_cast(text[end++]) & 0x3f); if (end - i == static_cast(n)) { i = end; c = value; } // Some legacy labels include a trailing NUL; SDL's debug font hid it. - if (c >= 32 && c != 127) out.push_back(c); + if (c >= 32 && c != 127) visit(c); } - return out; } #ifdef AVA2_HAS_FREETYPE struct Glyph { @@ -108,12 +109,12 @@ int TextFontSize(const std::string& name) { float MeasureText(const std::string& text, int size) { float width = 0; - for (unsigned code : Codepoints(text)) { + ForEachCodepoint(text, [&](unsigned code) { #ifdef AVA2_HAS_FREETYPE - if (Cache().face) { width += Cache().Get(size, code).advance; continue; } + if (Cache().face) { width += Cache().Get(size, code).advance; return; } #endif width += size * 0.5f; - } + }); return width; } @@ -122,7 +123,11 @@ void DrawText(SDL_Renderer* renderer, const std::string& text, int size, float x #ifdef AVA2_HAS_FREETYPE if (Cache().face) { auto& cache = TextureCache(renderer); - const std::string key = std::to_string(size) + ":" + text; + // Reused buffer: the hit path runs for every label every frame. + static std::string key; + char prefix[16]; + key.assign(prefix, std::snprintf(prefix, sizeof(prefix), "%d:", size)); + key += text; auto found = cache.entries.find(key); if (found != cache.entries.end()) { cache.recent.splice(cache.recent.begin(), cache.recent, found->second.recent); @@ -139,7 +144,7 @@ void DrawText(SDL_Renderer* renderer, const std::string& text, int size, float x if (!surface) return; SDL_FillSurfaceRect(surface, nullptr, 0); float pen = 0; - for (unsigned code : Codepoints(text)) { + ForEachCodepoint(text, [&](unsigned code) { const auto& glyph = Cache().Get(size, code); const int gx = int(std::round(pen * 2)) + glyph.left; const int gy = size * 2 - glyph.top; @@ -155,7 +160,7 @@ void DrawText(SDL_Renderer* renderer, const std::string& text, int size, float x } } pen += glyph.advance; - } + }); SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_DestroySurface(surface); if (texture) { diff --git a/src/eter_ui/PythonWindow.cpp b/src/eter_ui/PythonWindow.cpp index e2f024b0..320935f4 100644 --- a/src/eter_ui/PythonWindow.cpp +++ b/src/eter_ui/PythonWindow.cpp @@ -782,15 +782,16 @@ namespace UI SDL_Renderer* renderer = pDevice->GetRenderer(); if (!renderer) return; - std::string displayText = m_text; + std::string secretText; if (m_secret) - displayText.assign(m_text.length(), '*'); + secretText.assign(m_text.length(), '*'); + const std::string& displayText = m_secret ? secretText : m_text; if (displayText.empty() && !m_showCursor) return; const int fontSize = TextFontSize(m_font); - const float charWidth = MeasureText("*", fontSize); + const float charWidth = m_showCursor ? MeasureText("*", fontSize) : 0.0f; const float charHeight = float(fontSize); float textWidth = MeasureText(displayText, fontSize); float textHeight = charHeight; diff --git a/src/scripting/python_native_modules.cpp b/src/scripting/python_native_modules.cpp index 4c5b89ae..685441ce 100644 --- a/src/scripting/python_native_modules.cpp +++ b/src/scripting/python_native_modules.cpp @@ -615,16 +615,21 @@ static PyObject* texttail_register_item(PyObject*, PyObject* args) { void initTextTail() { // Use the same FreeType font path as UI text, preserving the Retina render // scale. SDL_RenderDebugText's fixed bitmap font is only a host fallback. + // One reused instance per callback: text tails paint several labels per actor per frame. + static auto fontFor=[](UI::CGraphicTextInstance& text,int& lastSize,float scale) { + const int size=int(12 * scale); + if(size!=lastSize){lastSize=size;text.SetFontName(("Tahoma:" + std::to_string(size)).c_str());} + }; TextTailManager::Instance().SetTextPainter( [](float x,float y,const std::string& value,uint32_t color,float scale,bool outline) { - UI::CGraphicTextInstance text; - text.SetFontName(("Tahoma:" + std::to_string(int(12 * scale))).c_str()); + static UI::CGraphicTextInstance text;static int lastSize=-1; + fontFor(text,lastSize,scale); text.SetValue(value.c_str()); text.SetFontColor(color); text.SetPosition(x,y); text.SetOutline(outline); text.Render(); }, [](const std::string& value,float scale) { - UI::CGraphicTextInstance text; - text.SetFontName(("Tahoma:" + std::to_string(int(12 * scale))).c_str()); + static UI::CGraphicTextInstance text;static int lastSize=-1; + fontFor(text,lastSize,scale); text.SetValue(value.c_str()); int width=0; text.GetTextSize(&width,nullptr); return float(width); }); static PyMethodDef methods[] = {