[perf][LOW] InputField hit-test is O(n^2): re-measures whole prefix per char #56

Closed
opened 2026-06-16 16:51:32 +02:00 by jorijnvdgraaf · 0 comments

Subsystem: InputField
Location: implementations/Crafter.Graphics-InputField.cpp:114-137
Impact: LOW · Effort: Small · Not hot path (no caller; mouse-down handler)

Problem

InputField_HitTestCursor loops i=1..size, each calling GetLineWidth(view(data,i)) which re-walks from byte 0 — O(n^2) stbtt_GetCodepointHMetrics calls (each an uncached cmap binary search).

Proposed fix

Single left-to-right cumulative-advance prefix-sum, then scan/binary-search.

Correctness caveat

The prefix-sum must be keyed at byte boundaries (cursorPos is byte-based via value.erase(cursorPos,1)), not per-codepoint. Note: this function currently has no caller in the repo.

**Subsystem:** InputField **Location:** `implementations/Crafter.Graphics-InputField.cpp:114-137` **Impact:** LOW · **Effort:** Small · Not hot path (no caller; mouse-down handler) ### Problem `InputField_HitTestCursor` loops `i=1..size`, each calling `GetLineWidth(view(data,i))` which re-walks from byte 0 — O(n^2) `stbtt_GetCodepointHMetrics` calls (each an uncached cmap binary search). ### Proposed fix Single left-to-right cumulative-advance prefix-sum, then scan/binary-search. ### Correctness caveat The prefix-sum must be keyed at **byte** boundaries (cursorPos is byte-based via `value.erase(cursorPos,1)`), not per-codepoint. Note: this function currently has no caller in the repo.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Catcrafts/Crafter.Graphics#56
No description provided.