Improve speed of drw_text when provided with large strings

Calculates len & ew in drw_font_getexts loop by incrementing instead of
decrementing; as such avoids proportional increase in time spent in loop
based on provided strings size.

Ref. https://git.suckless.org/dmenu/commit/c585e8e498ec6f9c423ab8ea07cf853ee5b05fbe.html#h0-0-3
This commit is contained in:
bakkeby 2021-08-16 10:40:43 +02:00
parent bd40208171
commit 3516ea6e65

7
drw.c
View File

@ -493,8 +493,11 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
if (utf8strlen) {
drw_font_getexts(usedfont, utf8str, utf8strlen, &ew, NULL);
/* shorten text if necessary */
for (len = MIN(utf8strlen, sizeof(buf) - 1); len && ew > w; len--)
drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
if (ew > w)
for (ew = 0, len = 0; ew < w - lpad * 2 && len < MIN(utf8strlen, sizeof(buf) - 1); len++)
drw_font_getexts(usedfont, utf8str, len, &ew, NULL);
else
len = MIN(utf8strlen, sizeof(buf) - 1);
if (len) {
memcpy(buf, utf8str, len);