diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2018-12-14 20:55:18 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2018-12-14 20:55:18 +0000 |
commit | ce87d1ef93cfa0f8fb749924abaef5329572418e (patch) | |
tree | 5e7a21170284b6eb3b4735c4edada28d19092e3f /lib | |
parent | 7b18a5a4ceff0514de182c2a998068ede9c4b142 (diff) |
Revisit the optimization for unbuffered I/O. We can use the buffer
passed to fread(3) directly in the FILE * and call __srefill() in
a loop without the memcpy(). This preserves the expected behavior
in all cases. OK semarie@, "This is neat" tedu@
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/stdio/fread.c | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index 2abd2ac8dea..ff132cf6523 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fread.c,v 1.16 2018/12/14 20:48:18 millert Exp $ */ +/* $OpenBSD: fread.c,v 1.17 2018/12/14 20:55:17 millert Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -68,6 +68,36 @@ fread(void *buf, size_t size, size_t count, FILE *fp) 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 && buf != NULL) { + /* set up the buffer */ + fp->_bf._base = fp->_p = buf; + fp->_bf._size = total; + + while (resid > 0) { + if (__srefill(fp)) { + /* no more input: return partial result */ + count = (total - resid) / size; + break; + } + fp->_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 > (r = fp->_r)) { (void)memcpy(p, fp->_p, r); fp->_p += r; |