From owner-svn-src-head@freebsd.org Tue Jun 13 12:35:02 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 2950CBF3432; Tue, 13 Jun 2017 12:35:02 +0000 (UTC) (envelope-from gahr@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 EDD557A311; Tue, 13 Jun 2017 12:35:01 +0000 (UTC) (envelope-from gahr@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v5DCZ1Ym077438; Tue, 13 Jun 2017 12:35:01 GMT (envelope-from gahr@FreeBSD.org) Received: (from gahr@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v5DCZ1aR077437; Tue, 13 Jun 2017 12:35:01 GMT (envelope-from gahr@FreeBSD.org) Message-Id: <201706131235.v5DCZ1aR077437@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: gahr set sender to gahr@FreeBSD.org using -f From: Pietro Cerutti Date: Tue, 13 Jun 2017 12:35:01 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r319897 - head/usr.bin/yes X-SVN-Group: head 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.23 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: Tue, 13 Jun 2017 12:35:02 -0000 Author: gahr (ports committer) Date: Tue Jun 13 12:35:01 2017 New Revision: 319897 URL: https://svnweb.freebsd.org/changeset/base/319897 Log: Improve yes' throughput On my system, this brings up the throughput from ~20 to ~600 MiB/s. Inspired by: https://www.reddit.com/r/unix/comments/6gxduc/how_is_gnu_yes_so_fast/ Reviewed by: cognet Approved by: cognet Modified: head/usr.bin/yes/yes.c Modified: head/usr.bin/yes/yes.c ============================================================================== --- head/usr.bin/yes/yes.c Tue Jun 13 12:07:18 2017 (r319896) +++ head/usr.bin/yes/yes.c Tue Jun 13 12:35:01 2017 (r319897) @@ -44,20 +44,42 @@ static const char rcsid[] = "$FreeBSD$"; #include #include #include +#include +#include int main(int argc, char **argv) { + char buf[8192]; + char y[2] = { 'y', '\n' }; + char * exp = y; + size_t buflen = 0; + size_t explen = sizeof(y); if (caph_limit_stdio() < 0 || (cap_enter() < 0 && errno != ENOSYS)) err(1, "capsicum"); if (argc > 1) - while (puts(argv[1]) != EOF) - ; - else - while (puts("y") != EOF) - ; + { + exp = argv[1]; + explen = strlen(exp) + 1; + exp[explen - 1] = '\n'; + } + + if (explen <= sizeof(buf)) + { + while (buflen < sizeof(buf) - explen) + { + memcpy(buf + buflen, exp, explen); + buflen += explen; + } + exp = buf; + explen = buflen; + } + + while (write(STDOUT_FILENO, exp, explen) > 0) + ; + err(1, "stdout"); /*NOTREACHED*/ }