[hackers] [st][PATCH] separate blinking timer from drawing in run()

From: kais euchi <kais.euchi_AT_gmail.com>
Date: Wed, 2 Jan 2019 02:50:15 +0100

Hello everyone,

This article [0] made me wonder how to improve latency in st, and i
thought i would share this small modification for a non-blinking setup.
When blinktimeout is set to 0, it reduces latency by ca. 5ms [1] by avoiding
useless delay calculation making it also independent from the xfps setting.

Regards,
Kais

[0] : https://lwn.net/Articles/751763/
[1] using typometer, found here : https://pavelfatin.com/typing-with-pleasure/

---
 x.c | 75 ++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 40 insertions(+), 35 deletions(-)
diff --git a/x.c b/x.c
index 0422421..7afbab5 100644
--- a/x.c
+++ b/x.c
_AT_@ -1816,25 +1816,48 @@ run(void)
 		if (FD_ISSET(xfd, &rfd))
 			xev = actionfps;
 
-		clock_gettime(CLOCK_MONOTONIC, &now);
-		drawtimeout.tv_sec = 0;
-		drawtimeout.tv_nsec =  (1000 * 1E6)/ xfps;
-		tv = &drawtimeout;
-
-		dodraw = 0;
-		if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
-			tsetdirtattr(ATTR_BLINK);
-			win.mode ^= MODE_BLINK;
-			lastblink = now;
-			dodraw = 1;
-		}
-		deltatime = TIMEDIFF(now, last);
-		if (deltatime > 1000 / (xev ? xfps : actionfps)) {
-			dodraw = 1;
-			last = now;
+		if (blinktimeout) {
+			clock_gettime(CLOCK_MONOTONIC, &now);
+			drawtimeout.tv_sec = 0;
+			drawtimeout.tv_nsec =  (1000 * 1E6)/ xfps;
+			tv = &drawtimeout;
+
+			dodraw = 0;
+			if (TIMEDIFF(now, lastblink) > blinktimeout) {
+				tsetdirtattr(ATTR_BLINK);
+				win.mode ^= MODE_BLINK;
+				lastblink = now;
+				dodraw = 1;
+			}
+			deltatime = TIMEDIFF(now, last);
+			if (deltatime > 1000 / (xev ? xfps : actionfps)) {
+				dodraw = 1;
+				last = now;
+			}
+
+			if (dodraw) {
+				if (!FD_ISSET(ttyfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
+					if (blinkset) {
+						if (TIMEDIFF(now, lastblink) \
+								> blinktimeout) {
+							drawtimeout.tv_nsec = 1000;
+						} else {
+							drawtimeout.tv_nsec = (1E6 * \
+									(blinktimeout - \
+									 TIMEDIFF(now,
+										 lastblink)));
+						}
+						drawtimeout.tv_sec = \
+								     drawtimeout.tv_nsec / 1E9;
+						drawtimeout.tv_nsec %= (long)1E9;
+					} else {
+						tv = NULL;
+					}
+				}
+			}
 		}
 
-		if (dodraw) {
+		if (dodraw || !blinktimeout) {
 			while (XPending(xw.dpy)) {
 				XNextEvent(xw.dpy, &ev);
 				if (XFilterEvent(&ev, None))
_AT_@ -1848,24 +1871,6 @@ run(void)
 
 			if (xev && !FD_ISSET(xfd, &rfd))
 				xev--;
-			if (!FD_ISSET(ttyfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
-				if (blinkset) {
-					if (TIMEDIFF(now, lastblink) \
-							> blinktimeout) {
-						drawtimeout.tv_nsec = 1000;
-					} else {
-						drawtimeout.tv_nsec = (1E6 * \
-							(blinktimeout - \
-							TIMEDIFF(now,
-								lastblink)));
-					}
-					drawtimeout.tv_sec = \
-					    drawtimeout.tv_nsec / 1E9;
-					drawtimeout.tv_nsec %= (long)1E9;
-				} else {
-					tv = NULL;
-				}
-			}
 		}
 	}
 }
-- 
2.20.1
Received on Wed Jan 02 2019 - 02:50:15 CET

This archive was generated by hypermail 2.3.0 : Wed Jan 02 2019 - 03:00:22 CET