Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-9]: src/lib/libc/stdio Pull up following revision(s) (requested b...



details:   https://anonhg.NetBSD.org/src/rev/2cd81b4cb67a
branches:  netbsd-9
changeset: 951914:2cd81b4cb67a
user:      martin <martin%NetBSD.org@localhost>
date:      Sun Feb 07 17:41:02 2021 +0000

description:
Pull up following revision(s) (requested by jdolecek in ticket #1198):

        lib/libc/stdio/fread.c: revision 1.24 (via patch)

for unbuffered I/O arrange for the destination buffer to be filled in one
go, instead of triggering long series of 1 byte read(2)s; this speeds up
fread() several order of magnitudes for this case, directly proportional
to the size of the supplied buffer
change adapted from OpenBSD rev. 1.19

fixes PR lib/55808 by Roland Illig

diffstat:

 lib/libc/stdio/fread.c |  34 ++++++++++++++++++++++++++++++++--
 1 files changed, 32 insertions(+), 2 deletions(-)

diffs (55 lines):

diff -r 964301f2b05c -r 2cd81b4cb67a lib/libc/stdio/fread.c
--- a/lib/libc/stdio/fread.c    Sun Feb 07 16:43:30 2021 +0000
+++ b/lib/libc/stdio/fread.c    Sun Feb 07 17:41:02 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fread.c,v 1.22 2012/03/15 18:22:30 christos Exp $      */
+/*     $NetBSD: fread.c,v 1.22.34.1 2021/02/07 17:41:02 martin Exp $   */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)fread.c    8.2 (Berkeley) 12/11/93";
 #else
-__RCSID("$NetBSD: fread.c,v 1.22 2012/03/15 18:22:30 christos Exp $");
+__RCSID("$NetBSD: fread.c,v 1.22.34.1 2021/02/07 17:41:02 martin Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -72,6 +72,36 @@
                fp->_r = 0;
        total = resid;
        p = buf;
+
+       /*
+        * If we're unbuffered we know that the buffer in fp is empty so
+        * we can read directly into buf.  This is much faster than a
+        * series of one byte reads into fp->_nbuf.
+        */
+       if ((fp->_flags & __SNBF) != 0) {
+               while (resid > 0) {
+                       /* set up the buffer */
+                       fp->_bf._base = fp->_p = (unsigned char *)p;
+                       fp->_bf._size = resid;
+
+                       if (__srefill(fp)) {
+                               /* no more input: return partial result */
+                               count = (total - resid) / size;
+                               break;
+                       }
+                       p += fp->_r;
+                       resid -= fp->_r;
+               }
+
+               /* restore the old buffer (see __smakebuf) */
+               fp->_bf._base = fp->_p = fp->_nbuf;
+               fp->_bf._size = 1;
+               fp->_r = 0;
+
+               FUNLOCKFILE(fp);
+               return (count);
+       }
+
        while (resid > (size_t)(r = fp->_r)) {
                (void)memcpy(p, fp->_p, (size_t)r);
                fp->_p += r;



Home | Main Index | Thread Index | Old Index