~nabijaczleweli/voreutils

c331c7b6c1653826022713b7e2cc7b11da3dd34e — наб a month ago 37ec3b5
Linux sendfile() is literal magic
3 files changed, 24 insertions(+), 3 deletions(-)

M README.md
M cmd/cat.cpp
M cmd/head.cpp
M README.md => README.md +1 -1
@@ 6,7 6,7 @@ This probably wants a better blurb.
## [Manual](//srhtcdn.githack.com/~nabijaczleweli/voreutils/blob/man/man0/index.0.html)

GNU coreutils provide the following 106 binaries, according to `dpkg -L coreutils | grep bin/` on Bullseye (8.32-4+b1):
  * ☑ /bin/cat – actually faster for raw catting to/from pipes and to/from files, `splice(2)`s and `copy_file_range(2)`s by default
  * ☑ /bin/cat – actually faster for raw catting to/from pipes/files/blockdevs under Linux: `splice(2)`s/`copy_file_range(2)`s/`sendfile(2)`s by default
  * ☑ /bin/chgrp
  * ☐ /bin/chmod
  * ☑ /bin/chown

M cmd/cat.cpp => cmd/cat.cpp +12 -2
@@ 14,6 14,9 @@
#include <vore-print>
#include <vore-span>
#include <vore-stdio>
#ifdef __linux__
#include <sys/sendfile.h>
#endif


using namespace std::literals;


@@ 170,6 173,14 @@ int main(int argc, const char * const * argv) {
				continue;
		}

		if(should_alt) {
			ssize_t rd;
			while((rd = sendfile(1, fd, nullptr, 128 * 1024 * 1024)) > 0)
				;
			if(rd != -1)  // All errors are better-served when detected by the read()/fwrite() loop below
				continue;
		}

		should_alt = false;
#endif



@@ 177,8 188,7 @@ int main(int argc, const char * const * argv) {
			while((rd = read(fd, buf, sizeof(buf))) == -1 && errno == EINTR)
				;
			if(rd == -1) {
				std::fprintf(stderr, "%s: %s: %s\n", argv[0], file, std::strerror(errno));
				err = true;
				std::fprintf(stderr, "%s: %s: %s\n", argv[0], file, std::strerror(errno)), err = true;
				break;
			}
			if(!rd)

M cmd/head.cpp => cmd/head.cpp +11 -0
@@ 15,6 15,9 @@
#include <vore-print>
#include <vore-size>
#include <vore-stdio>
#ifdef __linux__
#include <sys/sendfile.h>
#endif

using namespace std::literals;
using namespace vore::literals;


@@ 202,6 205,14 @@ int main(int argc, const char * const * argv) {
						if(rd != -1)  // All errors are better-served when detected by the read()/write() loop below
							continue;
					}

					{
						ssize_t rd{};
						while(left && (rd = sendfile(1, fd, nullptr, std::min(128_u64 * 1024_u64 * 1024_u64, left))) > 0)
							left -= rd;
						if(rd != -1)  // All errors are better-served when detected by the read()/write() loop below
							continue;
					}
#endif

					for(ssize_t rd; left && (rd = read_all(fd, buf, std::min(static_cast<std::uint64_t>(sizeof(buf)), left)));) {