Skip to content

fast_io.rs: use splice if copy_file_range is not supported - #459

Open
Franklin-Qi wants to merge 2 commits into
uutils:mainfrom
Franklin-Qi:fix-#443-use-sendfile-or-splice-if-copy-file-range-is-not-supported
Open

Franklin-Qi wants to merge 2 commits into
uutils:mainfrom
Franklin-Qi:fix-#443-use-sendfile-or-splice-if-copy-file-range-is-not-supported

Conversation

@Franklin-Qi

@Franklin-Qi Franklin-Qi commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

fast_io.rs: use splice if copy_file_range is not supported

  • 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.
  • If splice becomes unsupported after some bytes were already sent, return Some(copied) so the write fallback only emits the remainder.

Closes: #443

@codecov

codecov Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.14056% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.93%. Comparing base (f7fb0d7) to head (49fe557).

Files with missing lines Patch % Lines
src/sed/fast_io.rs 85.02% 37 Missing ⚠️
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              
Flag Coverage Δ
macos_latest 85.00% <92.00%> (+0.02%) ⬆️
ubuntu_latest 85.20% <85.36%> (+0.03%) ⬆️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Franklin-Qi
Franklin-Qi force-pushed the fix-#443-use-sendfile-or-splice-if-copy-file-range-is-not-supported branch from cee18ce to 581e725 Compare June 5, 2026 07:57
@oech3

oech3 commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

sendfile is internally splice. So no worth to try it. splice with middler pipe is faster.

@oech3

oech3 commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Please use rustix instead of unsafe libc.

Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
@sylvestre

Copy link
Copy Markdown
Contributor

@Franklin-Qi sorry, it needs to be rebased

@Franklin-Qi
Franklin-Qi force-pushed the fix-#443-use-sendfile-or-splice-if-copy-file-range-is-not-supported branch from 581e725 to bd5b0e2 Compare August 12, 2026 00:35
@codspeed

codspeed Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing Franklin-Qi:fix-#443-use-sendfile-or-splice-if-copy-file-range-is-not-supported (49fe557) with main (f7fb0d7)

Open in CodSpeed

@Franklin-Qi
Franklin-Qi force-pushed the fix-#443-use-sendfile-or-splice-if-copy-file-range-is-not-supported branch 5 times, most recently from 7551756 to 1889db5 Compare August 12, 2026 01:34
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
@oech3

oech3 commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

remove sendfile from PR's title and the commit message.

@sylvestre

Copy link
Copy Markdown
Contributor

@Franklin-Qi ping?

@Franklin-Qi

Copy link
Copy Markdown
Contributor Author

@Franklin-Qi ping?

Yeah, I will deal with this issue soon. @sylvestre

Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
Comment thread src/sed/fast_io.rs Resolved
- 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
@Franklin-Qi
Franklin-Qi force-pushed the fix-#443-use-sendfile-or-splice-if-copy-file-range-is-not-supported branch from 1889db5 to 9628ac1 Compare September 30, 2026 11:03
@Franklin-Qi Franklin-Qi changed the title fast_io.rs: use sendfile or splice if copy_file_range is not supported fast_io.rs: use splice if copy_file_range is not supported Sep 30, 2026
If splice becomes unsupported after some bytes were already sent,
return Some(copied) so the write fallback only emits the remainder.
@Franklin-Qi
Franklin-Qi force-pushed the fix-#443-use-sendfile-or-splice-if-copy-file-range-is-not-supported branch from 9628ac1 to 49fe557 Compare September 30, 2026 11:22
Comment thread src/sed/fast_io.rs
out_fd: BorrowedFd<'_>,
len: usize,
) -> std::io::Result<Option<usize>> {
let (pipe_rd, pipe_wr) = match rustix_pipe() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you use std::io::pipe and drop rustix_pipe?

Comment thread src/sed/fast_io.rs
/// Returns `None` if splice is not supported.
#[cfg(target_os = "linux")]
fn splice_via_middle_pipe(
in_fd: BorrowedFd<'_>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use impl AsFd for simplicity?

Comment thread src/sed/fast_io.rs
)?
} 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// No need to pre-check for a FIFO — splice fails with EINVAL otherwise.

Comment thread src/sed/fast_io.rs
{
if !self.pipe_enlarged {
let _ =
fcntl_setpipe_size(self.fast_copy.as_fd(), MAX_ROOTLESS_PIPE_SIZE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment thread src/sed/fast_io.rs
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not need to use unsafe if we used rustix by proper way.

Comment thread src/sed/fast_io.rs
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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you split PR?

Comment thread src/sed/fast_io.rs
Comment on lines +978 to +981
matches!(
err.raw_os_error(),
Some(libc::ENOSYS) | Some(libc::EOPNOTSUPP) | Some(libc::EINVAL) | Some(libc::EXDEV)
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about .is_some_and(|e| matches!(..))?

Comment thread src/sed/fast_io.rs
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.into() should work.

Comment thread src/sed/compiler.rs
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has nothing to do with splice, please move it to a separate PR

Comment thread src/sed/fast_io.rs
chunk.len,
)? {
Some(n) if n == chunk.len => n,
Some(n) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what finish_with_write does, could it be reused here?

Comment thread src/sed/fast_io.rs
None => reliable_write(self.fast_copy.fd, chunk.out_ptr, chunk.len)?,
}
}
#[cfg(not(target_os = "linux"))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same as the else branch below, could we avoid the duplication?

Comment thread src/sed/fast_io.rs

#[cfg(target_os = "linux")]
#[test]
fn test_reliable_copy_file_range_basic() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_reliable_copy_file_range_rustix_path already covers this, no?

Comment thread src/sed/fast_io.rs
{
finish_with_write(data.as_ptr(), outfile.as_fd(), data.len(), already).unwrap();
}
#[cfg(not(target_os = "linux"))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this branch only tests reliable_write, which already has its own tests. could the test just be linux-only?

Comment thread tests/by-util/test_sed.rs
/// 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"))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unix is redundant here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fast_io.rs: splice if copy_file_range is not supported

3 participants