inputw: improve correctness and startup performance 77526f75
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.
NRK · 2022-03-24 00:37 1 file(s) · +9 −10
dmenu.c +9 −10
547 547
readstdin(void)
548 548
{
549 549
	char buf[sizeof text], *p;
550 -
	size_t i, imax = 0, size = 0;
551 -
	unsigned int tmpmax = 0;
550 +
	size_t i, size = 0;
552 551
553 552
	/* read each line from stdin and add it to the item list */
554 553
	for (i = 0; fgets(buf, sizeof buf, stdin); i++) {
560 559
		if (!(items[i].text = strdup(buf)))
561 560
			die("cannot strdup %u bytes:", strlen(buf) + 1);
562 561
		items[i].out = 0;
563 -
		drw_font_getexts(drw->fonts, buf, strlen(buf), &tmpmax, NULL);
564 -
		if (tmpmax > inputw) {
565 -
			inputw = tmpmax;
566 -
			imax = i;
567 -
		}
568 562
	}
569 563
	if (items)
570 564
		items[i].text = NULL;
571 -
	inputw = items ? TEXTW(items[imax].text) : 0;
572 565
	lines = MIN(lines, i);
573 566
}
574 567
614 607
setup(void)
615 608
{
616 609
	int x, y, i, j;
617 -
	unsigned int du;
610 +
	unsigned int du, tmp;
618 611
	XSetWindowAttributes swa;
619 612
	XIM xim;
620 613
	Window w, dw, *dws;
621 614
	XWindowAttributes wa;
622 615
	XClassHint ch = {"dmenu", "dmenu"};
616 +
	struct item *item;
623 617
#ifdef XINERAMA
624 618
	XineramaScreenInfo *info;
625 619
	Window pw;
677 671
		mw = wa.width;
678 672
	}
679 673
	promptw = (prompt && *prompt) ? TEXTW(prompt) - lrpad / 4 : 0;
680 -
	inputw = MIN(inputw, mw/3);
674 +
	for (item = items; item && item->text; ++item) {
675 +
		if ((tmp = textw_clamp(item->text, mw/3)) > inputw) {
676 +
			if ((inputw = tmp) == mw/3)
677 +
				break;
678 +
		}
679 +
	}
681 680
	match();
682 681
683 682
	/* create menu window */