From 761bc7939f7ff32a1a95d4c2150dc488806b9823 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Mon, 28 Mar 2022 11:10:31 +0200 Subject: [PATCH] inputw: improve correctness and startup performance a massive amount of time inside readstdin() is spent trying to get the max input width and then put it into inputw, only for it to get clamped down to mw/3 inside setup(). it makes more sense to calculate inputw inside setup() once we have mw available. similar to the last patch, i see noticeable startup performance improvement: before -> after 160ms -> 60ms additionally this will take fallback fonts into account compared to the previous version, so it's not only more performant but also more correct. ref. https://git.suckless.org/dmenu/commit/77526f756e23e362081ac807521f901f2e5cd5e6.html Disclaimer: this may break the JSON patch --- dmenu.c | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/dmenu.c b/dmenu.c index fa879fd..46c933a 100644 --- a/dmenu.c +++ b/dmenu.c @@ -1358,12 +1358,10 @@ readstdin(void) char buf[sizeof text], *p; #if JSON_PATCH size_t i; - unsigned int imax = 0; struct item *item; #else - size_t i, imax = 0, size = 0; + size_t i, size = 0; #endif // JSON_PATCH - unsigned int tmpmax = 0; #if PASSWORD_PATCH if (passwd) { @@ -1415,19 +1413,6 @@ readstdin(void) #if HIGHPRIORITY_PATCH items[i].hp = arrayhas(hpitems, hplength, items[i].text); #endif // HIGHPRIORITY_PATCH - #if PANGO_PATCH - drw_font_getexts(drw->font, buf, strlen(buf), &tmpmax, NULL, True); - #else - drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL); - #endif // PANGO_PATCH - if (tmpmax > inputw) { - inputw = tmpmax; - #if JSON_PATCH - imax = items_ln - 1; - #else - imax = i; - #endif // JSON_PATCH - } } if (items) #if JSON_PATCH @@ -1435,11 +1420,6 @@ readstdin(void) #else items[i].text = NULL; #endif // JSON_PATCH - #if PANGO_PATCH - inputw = items ? TEXTWM(items[imax].text) : 0; - #else - inputw = items ? TEXTW(items[imax].text) : 0; - #endif // PANGO_PATCH #if JSON_PATCH lines = MIN(lines, items_ln); #else @@ -1514,12 +1494,13 @@ static void setup(void) { int x, y, i, j; - unsigned int du; + unsigned int du, tmp; XSetWindowAttributes swa; XIM xim; Window w, dw, *dws; XWindowAttributes wa; XClassHint ch = {"dmenu", "dmenu"}; + struct item *item; #ifdef XINERAMA XineramaScreenInfo *info; Window pw; @@ -1656,7 +1637,12 @@ setup(void) promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0; #endif // PANGO_PATCH #endif // CENTER_PATCH - inputw = MIN(inputw, mw/3); + for (item = items; item && item->text; ++item) { + if ((tmp = textw_clamp(item->text, mw/3)) > inputw) { + if ((inputw = tmp) == mw/3) + break; + } + } match(); /* create menu window */