From 129cd2bc482fde04bf63cc18c4ab4ec1ddf4e9c0 Mon Sep 17 00:00:00 2001 From: Eunchurn Park Date: Thu, 25 Dec 2025 15:25:36 +0900 Subject: [PATCH] Optimize glyph lookup with binary search for Korean fonts Replace linear O(n) search with binary search O(log n) for unicode interval lookup. Korean fonts have many intervals (~30,000+ glyphs), so this improves text rendering performance during page navigation. --- lib/EpdFont/EpdFont.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/EpdFont/EpdFont.cpp b/lib/EpdFont/EpdFont.cpp index 0f53f95..661317d 100644 --- a/lib/EpdFont/EpdFont.cpp +++ b/lib/EpdFont/EpdFont.cpp @@ -59,14 +59,28 @@ bool EpdFont::hasPrintableChars(const char* string) const { const EpdGlyph* EpdFont::getGlyph(const uint32_t cp) const { const EpdUnicodeInterval* intervals = data->intervals; - for (int i = 0; i < data->intervalCount; i++) { - const EpdUnicodeInterval* interval = &intervals[i]; - if (cp >= interval->first && cp <= interval->last) { + const int count = data->intervalCount; + + if (count == 0) return nullptr; + + // Binary search for O(log n) lookup instead of O(n) + // Critical for Korean fonts with many unicode intervals + int left = 0; + int right = count - 1; + + while (left <= right) { + const int mid = left + (right - left) / 2; + const EpdUnicodeInterval* interval = &intervals[mid]; + + if (cp < interval->first) { + right = mid - 1; + } else if (cp > interval->last) { + left = mid + 1; + } else { + // Found: cp >= interval->first && cp <= interval->last return &data->glyph[interval->offset + (cp - interval->first)]; } - if (cp < interval->first) { - return nullptr; - } } + return nullptr; }