From d92f64bad488975b4675afbfac0d9832a4fd17e9 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Mon, 8 Dec 2014 20:40:54 +0000 Subject: don't do silly (and slow) one byte reads in unbuffered mode. from enh at google --- lib/libc/stdio/fread.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'lib/libc') diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index 8a592f6d3f1..6e957623e1f 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fread.c,v 1.12 2014/05/01 16:40:36 deraadt Exp $ */ +/* $OpenBSD: fread.c,v 1.13 2014/12/08 20:40:53 tedu Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -68,6 +68,21 @@ fread(void *buf, size_t size, size_t count, FILE *fp) fp->_r = 0; total = resid; p = buf; + + if ((fp->_flags & __SNBF) != 0) { + /* + * We know if we're unbuffered that our buffer is empty, so + * we can just read directly. This is much faster than the + * loop below which will perform a series of one byte reads. + */ + while (resid > 0 && (r = (*fp->_read)(fp->_cookie, p, resid)) > 0) { + p += r; + resid -= r; + } + FUNLOCKFILE(fp); + return ((total - resid) / size); + } + while (resid > (r = fp->_r)) { (void)memcpy((void *)p, (void *)fp->_p, (size_t)r); fp->_p += r; -- cgit v1.2.3