svn commit: r319897 - head/usr.bin/yes

Bryan Drewery bdrewery at FreeBSD.org
Tue Jun 13 16:37:54 UTC 2017


On 6/13/2017 5:35 AM, Pietro Cerutti wrote:
> 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 <capsicum_helpers.h>
>  #include <err.h>
>  #include <stdio.h>
> +#include <string.h>
> +#include <unistd.h>
>  
>  int
>  main(int argc, char **argv)
>  {
> +	char buf[8192];

8192 feels arbitrary but it has a purpose.  Shouldn't it be based on
PAGE_SIZE or something?

> +	char y[2] = { 'y', '\n' };
> +	char * exp = y;
> +	size_t buflen = 0;
> +	size_t explen = sizeof(y);

More style bugs here

>  
>  	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*/
>  }
> 


-- 
Regards,
Bryan Drewery

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freebsd.org/pipermail/svn-src-head/attachments/20170613/37b478a7/attachment.sig>


More information about the svn-src-head mailing list