summaryrefslogtreecommitdiffstats
path: root/usr.bin
diff options
context:
space:
mode:
authorkevans <kevans@FreeBSD.org>2018-04-13 03:32:18 +0000
committerkevans <kevans@FreeBSD.org>2018-04-13 03:32:18 +0000
commite815c44a7971dcfcd1f7fde4db8f92f11057f703 (patch)
tree077ea301d4968261753a4a622e24c95d60072307 /usr.bin
parent290514978463c1489e8a1914541f6babc3b49f8a (diff)
downloadFreeBSD-src-e815c44a7971dcfcd1f7fde4db8f92f11057f703.zip
FreeBSD-src-e815c44a7971dcfcd1f7fde4db8f92f11057f703.tar.gz
MFC r319897-r319898, r319904: Improve yes' throughput
r319897: 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/ r319898: Handle partial writes r319904: style(9) fixes.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/yes/yes.c35
1 files changed, 29 insertions, 6 deletions
diff --git a/usr.bin/yes/yes.c b/usr.bin/yes/yes.c
index 2336d3f..404bf66 100644
--- a/usr.bin/yes/yes.c
+++ b/usr.bin/yes/yes.c
@@ -44,20 +44,43 @@ 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];
+ char y[2] = { 'y', '\n' };
+ char * exp = y;
+ size_t buflen = 0;
+ size_t explen = sizeof(y);
+ size_t more;
+ ssize_t ret;
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)
- ;
+ if (argc > 1) {
+ 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;
+ }
+
+ more = explen;
+ while ((ret = write(STDOUT_FILENO, exp + (explen - more), more)) > 0)
+ if ((more -= ret) == 0)
+ more = explen;
+
err(1, "stdout");
/*NOTREACHED*/
}
OpenPOWER on IntegriCloud