Skip to content

Move std::io::copy to alloc::io - #158548

Open
bushrat011899 wants to merge 2 commits into
rust-lang:mainfrom
bushrat011899:alloc_io_copy_internals
Open

Move std::io::copy to alloc::io#158548
bushrat011899 wants to merge 2 commits into
rust-lang:mainfrom
bushrat011899:alloc_io_copy_internals

Conversation

@bushrat011899

@bushrat011899 bushrat011899 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

View all comments

ACP: rust-lang/libs-team#755
Tracking issue: #154046
Split From: #156527
Blocked On: #158547

Description

Moves std::io::copy into alloc::io. Blocked on #158547.

This relies on specialization to allow std to provide optimised copy implementations for its types where appropriate. The exact technique involves defining a new trait, alloc::io::SpecCopy:

#[doc(hidden)]
#[unstable(feature = "core_io_internals", reason = "exposed only for libstd", issue = "none")]
#[rustc_specialization_trait]
pub trait SpecCopy: Read {
    /// Attempt to copy from this reader to the provided writer using a specialized
    /// process.
    fn copy<R: Read + ?Sized, W: Write + ?Sized>(
        _reader: &mut R,
        _writer: &mut W,
    ) -> Result<CopyState>;
}

Since optimised copying requires both the reader and writer to support the operation between each other, we can choose one of them to be the implementer of the copy algorithm, and delegate specialization to it. In this case, I've chosen the reader to be the provider of the specialized copy implementation arbitrarily. Note that the SpecCopy::copy function is generic over the reader specifically to allow wrappers like Take<R> to be visible to the implementation of copy.

Because this introduces a new layer of specialization to io::copy, I think this PR should be benchmarked to make sure performance characteristics aren't too different. I am expecting compilation time to be slightly worse, since there's just more specialization happening, but the actual code run should be the same.


Notes

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Jun 29, 2026
@rustbot

rustbot commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

r? @clarfonthey

rustbot has assigned @clarfonthey.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 6 candidates

@bushrat011899

Copy link
Copy Markdown
Contributor Author

@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 29, 2026
@rust-bors

This comment has been minimized.

@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from 1bcdfe1 to 43f67ba Compare July 2, 2026 02:01
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch 2 times, most recently from 90ec159 to 8139972 Compare July 3, 2026 03:50
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from 8139972 to cb62676 Compare July 6, 2026 09:26
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from cb62676 to 3bb0364 Compare July 6, 2026 21:51
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from 3bb0364 to df9a018 Compare July 6, 2026 22:21
@rustbot

This comment has been minimized.

@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from df9a018 to 8f363d9 Compare July 7, 2026 05:28
@rustbot

This comment has been minimized.

@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from 8f363d9 to d331e98 Compare July 8, 2026 09:09
@bushrat011899

Copy link
Copy Markdown
Contributor Author

@rustbot label -T-compiler -T-rustdoc -T-rustdoc-frontend

@rustbot rustbot removed T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Jul 21, 2026
@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from 72de226 to b2a9ee4 Compare July 23, 2026 00:54
@rustbot

This comment has been minimized.

SpecCopyInner::copy((reader, writer))
}

trait SpecCopyInner {

@clarfonthey clarfonthey Jul 28, 2026

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.

Is there a reason why this has to go through a layer of indirection via specialized_copy instead of just being used directly?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah we can't directly call SpecCopy::copy since that would require a default blanket implementation of SpecCopy for all Read types, which doesn't work without another intermediate type for marking that specialization too. I also prefer to have a simple function like specialized_copy exported instead of SpecCopyInner::copy, since that trait purely exists as a specialization hack.

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.

Right, I guess what I meant was whether specialized_copy should exist instead of just calling SpecCopyInner::copy.

Comment thread library/alloc/src/io/copy.rs
Comment thread library/alloc/src/io/copy/specialization.rs
Comment thread library/alloc/src/io/copy/generic.rs
Comment thread library/alloc/src/io/copy/generic.rs Outdated
Comment thread library/alloc/src/io/copy/generic.rs
Comment thread library/alloc/src/io/copy/generic.rs Outdated
Comment thread library/alloc/src/io/copy/generic.rs Outdated
Comment thread library/alloc/src/io/copy/generic.rs
Comment thread library/alloc/src/io/copy/generic.rs
Comment thread library/alloc/src/io/copy/generic.rs Outdated
@clarfonthey

Copy link
Copy Markdown
Contributor

Left some comments as I try to wrap my brain around how these things work. Also going to want a perf run on the final versions since my guess is that these are going to be high-impact due to cross-crate inlining changes, but, we'll see if that's moot like it was for previous runs.

Rely on specialization to allow `std` to provide optimized copy implementations.
@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from b2a9ee4 to ab7f8ad Compare July 28, 2026 04:55
@rustbot

rustbot commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from ab7f8ad to 174d318 Compare July 28, 2026 05:05
@bushrat011899

Copy link
Copy Markdown
Contributor Author

Left some comments as I try to wrap my brain around how these things work. Also going to want a perf run on the final versions since my guess is that these are going to be high-impact due to cross-crate inlining changes, but, we'll see if that's moot like it was for previous runs.

I'm hopeful there wont be any performance loss with this move, but this is definitely the one move I would expect it to happen with, since specialization is very not ready for general use. I'm not even sure if cross-crate specialization is currently used at all. So yes please on a perf run!

@clarfonthey

Copy link
Copy Markdown
Contributor

(Mostly just wanted to clarify I was going to wait until the doc/etc. comments are fixed before running perf, but yes, we'll be doing that.)

Comment thread library/alloc/src/io/copy/generic.rs Outdated
Co-Authored-By: Clar Fon <15850505+clarfonthey@users.noreply.github.com>
@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from 174d318 to a0ff5e6 Compare July 28, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants