From owner-svn-src-head@freebsd.org Sat Dec 9 21:55:21 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 0CF9BE99C0F; Sat, 9 Dec 2017 21:55:21 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D9BA2774FE; Sat, 9 Dec 2017 21:55:20 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id vB9LtJth053386; Sat, 9 Dec 2017 21:55:19 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id vB9LtJFY053385; Sat, 9 Dec 2017 21:55:19 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201712092155.vB9LtJFY053385@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Sat, 9 Dec 2017 21:55:19 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r326736 - head/usr.bin/wc X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: head/usr.bin/wc X-SVN-Commit-Revision: 326736 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Dec 2017 21:55:21 -0000 Author: cem Date: Sat Dec 9 21:55:19 2017 New Revision: 326736 URL: https://svnweb.freebsd.org/changeset/base/326736 Log: wc(1): Extend non-controversial optimizations to '-c' mode wc(1)'s slow path for counting words or multibyte characters requires conversion of the 8-bit input stream to wide characters. However, a faster path can be used for counting only lines ('-l' -- newlines have the same representation in all supported encodings) or bytes ('-c'). The existing line count optimization was not used if the input was the implicit stdin. Additionally, it wasn't used if only byte counting was requested. This change expands the fast path to both of these scenarios. Expanding the buffer size from 64 kB helps reduce the number of read(2) calls needed, but exactly what impact that change has and what size to expand the buffer to are still under discussion. PR: 224160 Tested by: wosch (earlier version) Sponsored by: Dell EMC Isilon Modified: head/usr.bin/wc/wc.c Modified: head/usr.bin/wc/wc.c ============================================================================== --- head/usr.bin/wc/wc.c Sat Dec 9 21:04:56 2017 (r326735) +++ head/usr.bin/wc/wc.c Sat Dec 9 21:55:19 2017 (r326736) @@ -206,30 +206,30 @@ cnt(const char *file) linect = wordct = charct = llct = tmpll = 0; if (file == NULL) fd = STDIN_FILENO; - else { - if ((fd = open(file, O_RDONLY, 0)) < 0) { - xo_warn("%s: open", file); - return (1); - } - if (doword || (domulti && MB_CUR_MAX != 1)) - goto word; - /* - * Line counting is split out because it's a lot faster to get - * lines than to get words, since the word count requires some - * logic. - */ - if (doline) { - while ((len = read(fd, buf, MAXBSIZE))) { - if (len == -1) { - xo_warn("%s: read", file); - (void)close(fd); - return (1); - } - if (siginfo) { - show_cnt(file, linect, wordct, charct, - llct); - } - charct += len; + else if ((fd = open(file, O_RDONLY, 0)) < 0) { + xo_warn("%s: open", file); + return (1); + } + if (doword || (domulti && MB_CUR_MAX != 1)) + goto word; + /* + * Line counting is split out because it's a lot faster to get + * lines than to get words, since the word count requires some + * logic. + */ + if (doline || dochar) { + while ((len = read(fd, buf, MAXBSIZE))) { + if (len == -1) { + xo_warn("%s: read", file); + (void)close(fd); + return (1); + } + if (siginfo) { + show_cnt(file, linect, wordct, charct, + llct); + } + charct += len; + if (doline) { for (p = buf; len--; ++p) if (*p == '\n') { if (tmpll > llct) @@ -239,36 +239,37 @@ cnt(const char *file) } else tmpll++; } - reset_siginfo(); + } + reset_siginfo(); + if (doline) tlinect += linect; - if (dochar) - tcharct += charct; - if (dolongline) { - if (llct > tlongline) - tlongline = llct; - } + if (dochar) + tcharct += charct; + if (dolongline) { + if (llct > tlongline) + tlongline = llct; + } + show_cnt(file, linect, wordct, charct, llct); + (void)close(fd); + return (0); + } + /* + * If all we need is the number of characters and it's a + * regular file, just stat the puppy. + */ + if (dochar || domulti) { + if (fstat(fd, &sb)) { + xo_warn("%s: fstat", file); + (void)close(fd); + return (1); + } + if (S_ISREG(sb.st_mode)) { + reset_siginfo(); + charct = sb.st_size; show_cnt(file, linect, wordct, charct, llct); + tcharct += charct; (void)close(fd); return (0); - } - /* - * If all we need is the number of characters and it's a - * regular file, just stat the puppy. - */ - if (dochar || domulti) { - if (fstat(fd, &sb)) { - xo_warn("%s: fstat", file); - (void)close(fd); - return (1); - } - if (S_ISREG(sb.st_mode)) { - reset_siginfo(); - charct = sb.st_size; - show_cnt(file, linect, wordct, charct, llct); - tcharct += charct; - (void)close(fd); - return (0); - } } }