[ruby-core:105518] Re: [Ruby master Feature#18228] Add a `timeout` option to `IO.copy_stream`
From:
Eric Wong <normalperson@...>
Date:
2021-10-01 05:01:48 UTC
List:
ruby-core #105518
"ioquatix (Samuel Williams)" <noreply@ruby-lang.org> wrote:
> Just FYI: `sendfile` is less flexible and you should generally
> avoid it. The modern syscall is `splice`.
No point in avoiding sendfile, sendfile is considerably easier
to use in common cases and results in fewer syscalls.
splice requires one end to be a pipe; if neither end is a pipe
so you need to create and manage the pipe yourself as an
intermediate buffer. Basically, instead of:
void *buf = malloc(...);
while (read(rfd, buf, ...) > 0)
write(wfd, buf, ...);
free(buf);
It becomes:
int buf[2];
pipe2(buf, ...);
while (splice(rfd, ..., buf[1], ...) > 0) /* splice into pipe */
splice(buf[0], ..., wfd, ...); /* splice out of pipe */
close(buf[0]);
close(buf[1]);
sendfile creates an internal pipe transparently inside the
kernel for doing splice. The pipe is still there, but private
to the kernel so you won't have to jump between userspace and
kernel space repeatedly.
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>