From owner-dev-commits-src-all@freebsd.org Tue Jun 1 21:01:52 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E2F0865E7DD; Tue, 1 Jun 2021 21:01:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Fvl2m5vyKz4kwn; Tue, 1 Jun 2021 21:01:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B407F227D; Tue, 1 Jun 2021 21:01:52 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 151L1qRk094607; Tue, 1 Jun 2021 21:01:52 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 151L1qKF094606; Tue, 1 Jun 2021 21:01:52 GMT (envelope-from git) Date: Tue, 1 Jun 2021 21:01:52 GMT Message-Id: <202106012101.151L1qKF094606@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Pedro F. Giffuni" Subject: git: a45843c8ed7f - main - fread: improve performance for unbuffered reads MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pfg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a45843c8ed7fcc10c1f10d4346cc7b3c293c894b Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Jun 2021 21:01:53 -0000 The branch main has been updated by pfg: URL: https://cgit.FreeBSD.org/src/commit/?id=a45843c8ed7fcc10c1f10d4346cc7b3c293c894b commit a45843c8ed7fcc10c1f10d4346cc7b3c293c894b Author: Pedro F. Giffuni AuthorDate: 2021-05-31 01:48:38 +0000 Commit: Pedro F. Giffuni CommitDate: 2021-06-01 21:00:28 +0000 fread: improve performance for unbuffered reads We can use the buffer passed to fread(3) directly in the FILE *. The buffer needs to be reset before each call to __srefill(). This preserves the expected behavior in all cases. The change was found originally in OpenBSD and later adopted by NetBSD. MFC after: 2 weeks Obtained from: OpenBSD (CVS 1.18) Differential Revision: https://reviews.freebsd.org/D30548 --- lib/libc/stdio/fread.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index c12bcf1148b7..11f8d13f0caf 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -99,6 +99,35 @@ __fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict 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) { + while (resid > 0) { + /* set up the buffer */ + fp->_bf._base = fp->_p = 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; + + return (count); + } + while (resid > (r = fp->_r)) { (void)memcpy((void *)p, (void *)fp->_p, (size_t)r); fp->_p += r;