Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: ZSH performance regression in 5.8.1.2-test



On Mon, Apr 25, 2022 at 2:27 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> Theoretically we can block-read with impunity in cases 1 and 2 (anyone
> disagree?).  Testing for seek-ability would allow doing the "read too
> much and back up" trick in case 3.  I don't immediately see any way to
> avoid reading one byte at a time in case 4, does anyone have a
> suggestion?

Try this?
diff --git a/Src/input.c b/Src/input.c
index c59232681..f904427a0 100644
--- a/Src/input.c
+++ b/Src/input.c
@@ -217,12 +217,36 @@ shinbufrestore(void)
 static int
 shingetchar(void)
 {
-    int nread;
+    int nread, rsize = 1;
 
     if (shinbufptr < shinbufendptr)
 	return STOUC(*shinbufptr++);
-
+#ifdef HAVE_FSTAT
+    else {
+      struct stat st;
+      if (fstat(SHIN, &st) == 0 && !S_ISFIFO(st.st_mode))
+	  rsize = SHINBUFSIZE;
+    }
+#endif
     shinbufreset();
+    if (rsize > 1) {
+	do {
+	    errno = 0;
+	    nread = read(SHIN, shinbuffer, rsize);
+	} while (nread < 0 && errno == EINTR);
+	if (nread <= 0)
+	    return -1;
+	if (isset(SHINSTDIN) &&
+	    (shinbufendptr = memchr(shinbuffer, '\n', nread))) {
+	    shinbufendptr++;
+	    rsize = (shinbufendptr - shinbuffer);
+	    if (nread > rsize &&
+		lseek(SHIN, -(nread - rsize), SEEK_CUR) < 0)
+		zerr("lseek(%d, %d): %e", SHIN, -(nread - rsize), errno);
+	} else
+	    shinbufendptr = shinbuffer + nread;
+	return STOUC(*shinbufptr++);
+    }
     for (;;) {
        errno = 0;
        nread = read(SHIN, shinbufendptr, 1);


Messages sorted by: Reverse Date, Date, Thread, Author