4 ms·
I re-implemented it in C and for some reason O_APPEND is set on stdout by default. But aside from that it works just as the Rust version. #define _GNU_SOURC
by pantalaimon 8y ago
I re-implemented it in C and for some reason O_APPEND is set on stdout by default.
But aside from that it works just as the Rust version.
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BUF_SIZE 16384
static void unset_flag(int fd, int flag) {
int flags = fcntl(fd, F_GETFL, 0);
flags &= ~flags;
fcntl(fd, F_SETFL, flags);
}
int main(int argc, char** argv) {
int pipefd[2];
pipe(pipefd);
unset_flag(STDOUT_FILENO, O_APPEND);
for (int i = 1; i < argc; ++i) {
int fd = strcmp(argv[i], "-") ? open(argv[i], O_RDONLY) : STDIN_FILENO;
if (fd < 0) {
fprintf(stderr, "%s: No such file or directory\n", argv[i]);
exit(1);
}
while (splice(fd, NULL, pipefd[1], NULL, BUF_SIZE, 0))
splice(pipefd[0], NULL, STDOUT_FILENO, NULL, BUF_SIZE, 0);
close(fd);
}
return 0;
}
WTFPL if anyone cares.
- pantalaimon 8y agoTurns out the buffer size is significant: https://imgur.com/a/f4LiHVI https://imgur.com/a/f4LiHVI With 32kiB buffers I get double the throughput than with 16k, the peak appears to be at 64k, after that it levels off.