Repository navigation
fast_io.rs: use splice if copy_file_range is not supported - #459
Franklin-Qi wants to merge 2 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #459 +/- ##
==========================================
+ Coverage 83.86% 83.93% +0.06%
==========================================
Files 14 14
Lines 7203 7444 +241
Branches 424 438 +14
==========================================
+ Hits 6041 6248 +207
- Misses 1157 1191 +34
Partials 5 5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
cee18ce to
581e725
Compare
|
sendfile is internally splice. So no worth to try it. splice with middler pipe is faster. |
|
Please use rustix instead of unsafe libc. |
|
@Franklin-Qi sorry, it needs to be rebased |
581e725 to
bd5b0e2
Compare
7551756 to
1889db5
Compare
|
remove sendfile from PR's title and the commit message. |
|
@Franklin-Qi ping? |
Yeah, I will deal with this issue soon. @sylvestre |
- Prefer splice (via rustix) for file→pipe output, and enlarge the pipe buffer when possible. - Fall back in reliable_copy_file_range with copy_file_range → splice (middle pipe) → write, advancing in_ptr correctly after a partial copy. - Cover the paths with Linux unit tests and an end-to-end large-file pipe-through-sed test. Closes: uutils#443
1889db5 to
9628ac1
Compare
If splice becomes unsupported after some bytes were already sent, return Some(copied) so the write fallback only emits the remainder.
9628ac1 to
49fe557
Compare
| out_fd: BorrowedFd<'_>, | ||
| len: usize, | ||
| ) -> std::io::Result<Option<usize>> { | ||
| let (pipe_rd, pipe_wr) = match rustix_pipe() { |
There was a problem hiding this comment.
Would you use std::io::pipe and drop rustix_pipe?
| /// Returns `None` if splice is not supported. | ||
| #[cfg(target_os = "linux")] | ||
| fn splice_via_middle_pipe( | ||
| in_fd: BorrowedFd<'_>, |
There was a problem hiding this comment.
Can we use impl AsFd for simplicity?
| )? | ||
| } else if chunk.in_fast_copy.is_regular { | ||
| // File → non-regular (typically a pipe): try splice; fall back to write. | ||
| // No need to pre-check for a FIFO — splice fails with EINVAL otherwise. |
There was a problem hiding this comment.
| // No need to pre-check for a FIFO — splice fails with EINVAL otherwise. |
| { | ||
| if !self.pipe_enlarged { | ||
| let _ = | ||
| fcntl_setpipe_size(self.fast_copy.as_fd(), MAX_ROOTLESS_PIPE_SIZE); |
There was a problem hiding this comment.
| fcntl_setpipe_size(self.fast_copy.as_fd(), MAX_ROOTLESS_PIPE_SIZE); | |
| fcntl_setpipe_size(&self.fast_copy, MAX_ROOTLESS_PIPE_SIZE); |
This should work.
| fcntl_setpipe_size(self.fast_copy.as_fd(), MAX_ROOTLESS_PIPE_SIZE); | ||
| self.pipe_enlarged = true; | ||
| } | ||
| let in_off = unsafe { chunk.out_ptr.offset_from(chunk.base_ptr) } as u64; |
There was a problem hiding this comment.
We should not need to use unsafe if we used rustix by proper way.
| fn reliable_write(fd: i32, ptr: *const u8, len: usize) -> std::io::Result<usize> { | ||
| // A thin Write-compatible wrapper around a raw file descriptor | ||
| // This allows us to issue and utilize the write_all implementatin. | ||
| // This allows us to issue and utilize the write_all implementation. |
| matches!( | ||
| err.raw_os_error(), | ||
| Some(libc::ENOSYS) | Some(libc::EOPNOTSUPP) | Some(libc::EINVAL) | Some(libc::EXDEV) | ||
| ) |
There was a problem hiding this comment.
How about .is_some_and(|e| matches!(..))?
| match splice(in_fd, in_off, out_fd, out_off, len, SpliceFlags::empty()) { | ||
| Ok(n) => Ok(Some(n)), | ||
| Err(err) => { | ||
| let err = std::io::Error::from(err); |
| let msg = err.to_string(); | ||
|
|
||
| assert_eq!(msg, "input.txt:3:1: error: invalid command 'x'"); | ||
| // When stderr is a terminal, diagnostics may append an underlined |
There was a problem hiding this comment.
this has nothing to do with splice, please move it to a separate PR
| chunk.len, | ||
| )? { | ||
| Some(n) if n == chunk.len => n, | ||
| Some(n) => { |
There was a problem hiding this comment.
this is what finish_with_write does, could it be reused here?
| None => reliable_write(self.fast_copy.fd, chunk.out_ptr, chunk.len)?, | ||
| } | ||
| } | ||
| #[cfg(not(target_os = "linux"))] |
There was a problem hiding this comment.
this is the same as the else branch below, could we avoid the duplication?
|
|
||
| #[cfg(target_os = "linux")] | ||
| #[test] | ||
| fn test_reliable_copy_file_range_basic() { |
There was a problem hiding this comment.
test_reliable_copy_file_range_rustix_path already covers this, no?
| { | ||
| finish_with_write(data.as_ptr(), outfile.as_fd(), data.len(), already).unwrap(); | ||
| } | ||
| #[cfg(not(target_os = "linux"))] |
There was a problem hiding this comment.
this branch only tests reliable_write, which already has its own tests. could the test just be linux-only?
| /// End-to-end coverage for the Linux file→stdout(pipe) splice path: | ||
| /// a large regular file is read and written to a pipe without substitutions. | ||
| #[test] | ||
| #[cfg(all(unix, target_os = "linux"))] |
fast_io.rs: use splice if copy_file_range is not supported
Closes: #443