summaryrefslogtreecommitdiff
path: root/dmenu.c
diff options
context:
space:
mode:
authorNRK <nrk@disroot.org>2022-03-24 00:37:55 +0600
committerrafa_99 <raroma09@gmail.com>2022-03-25 23:12:39 +0000
commit38540c552941c3a348605d046cf27606afd5de98 (patch)
treebbb0a78c22d49ad7211027c697f03556c670ff25 /dmenu.c
parent1f6103dbc96515287a956172c6e751c04fd74904 (diff)
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.
Diffstat (limited to 'dmenu.c')
-rw-r--r--dmenu.c20
1 files changed, 9 insertions, 11 deletions
diff --git a/dmenu.c b/dmenu.c
index e496abf..0d41a2f 100644
--- a/dmenu.c
+++ b/dmenu.c
@@ -546,14 +546,12 @@ static void
readstdin(void)
{
char buf[sizeof text], *p;
- size_t i, imax = 0, size = 0;
- unsigned int tmpmax = 0;
+ size_t i, size = 0;
if(passwd){
inputw = lines = 0;
return;
}
-
/* read each line from stdin and add it to the item list */
for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
if (i + 1 >= size / sizeof *items)
@@ -564,15 +562,9 @@ readstdin(void)
if (!(items[i].text = strdup(buf)))
die("cannot strdup %u bytes:", strlen(buf) + 1);
items[i].out = 0;
- drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
- if (tmpmax > inputw) {
- inputw = tmpmax;
- imax = i;
- }
}
if (items)
items[i].text = NULL;
- inputw = items ? TEXTW(items[imax].text) : 0;
lines = MIN(lines, i);
}
@@ -618,12 +610,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;
@@ -681,7 +674,12 @@ setup(void)
mw = wa.width;
}
promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
- 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 */