ImGui: strong optimizations for color logger

Don't recalculate the length of each text on the lines displayed when pushing it to the draw list. Also use the already calculated length for intermediate operations.
This commit is contained in:
Kawe Mazidjatari 2025-02-09 00:34:15 +01:00
parent 1331c3c67b
commit d36ea4d769

View File

@ -340,7 +340,7 @@ CTextLogger::Coordinates CTextLogger::ScreenPosToCoordinates(const ImVec2& aPosi
buf[i++] = line.buffer[columnIndex++]; buf[i++] = line.buffer[columnIndex++];
buf[i] = '\0'; buf[i] = '\0';
columnWidth = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buf).x; columnWidth = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, buf, &buf[i]).x;
if (columnX + columnWidth * 0.5f > local.x) if (columnX + columnWidth * 0.5f > local.x)
break; break;
@ -859,17 +859,20 @@ void CTextLogger::Render()
if (!line.buffer.empty()) if (!line.buffer.empty())
{ {
const char* const text = line.buffer.c_str();
const char* const textEnd = &text[line.buffer.length()];
ImU32 color = line.color; ImU32 color = line.color;
if (m_itFilter.IsActive()) if (m_itFilter.IsActive())
{ {
// Make line dark if it isn't found by the filter // Make line dark if it isn't found by the filter
if (!m_itFilter.PassFilter(line.buffer.c_str())) if (!m_itFilter.PassFilter(text, textEnd))
color = 0xff605040; color = 0xff605040;
} }
const ImVec2 newOffset(textScreenPos.x, textScreenPos.y); const ImVec2 newOffset(textScreenPos.x, textScreenPos.y);
drawList->AddText(newOffset, color, line.buffer.c_str()); drawList->AddText(newOffset, color, text, textEnd);
} }
++lineNo; ++lineNo;
@ -1485,7 +1488,7 @@ float CTextLogger::TextDistanceToLineStart(const Coordinates& aFrom) const
tempCString[i] = line.buffer[it]; tempCString[i] = line.buffer[it];
tempCString[i] = '\0'; tempCString[i] = '\0';
distance += ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, tempCString, nullptr, nullptr).x; distance += ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, tempCString, &tempCString[i], nullptr).x;
} }
} }