Skip to content

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

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
bushrat011899:alloc_io_copy_internals
Aug 1, 2026
Merged

Move std::io::copy to alloc::io#158548
rust-bors[bot] merged 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
Comment thread library/alloc/src/io/copy/generic.rs Outdated
@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from 174d318 to a0ff5e6 Compare July 28, 2026 22:23
@clarfonthey

Copy link
Copy Markdown
Contributor

LGTM minus the extra comment about SpecCopyInner: #158548 (comment)

Co-Authored-By: Clar Fon <15850505+clarfonthey@users.noreply.github.com>
@bushrat011899
bushrat011899 force-pushed the alloc_io_copy_internals branch from a0ff5e6 to a8ef638 Compare August 1, 2026 04:54
@clarfonthey

Copy link
Copy Markdown
Contributor

@bors try
@rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 1, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 1, 2026
@rust-bors

rust-bors Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a168732 (a16873222e0640d7d31675ec6e963944dba0c3a6)
Base parent: cb9d1b0 (cb9d1b0640549f1b041aae430dc413ce93f8c204)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (a168732): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.3%, -0.2%] 5
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 0.5%, secondary 0.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.5% [0.4%, 0.6%] 4
Regressions ❌
(secondary)
0.9% [0.4%, 4.8%] 18
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.5% [0.4%, 0.6%] 4

Cycles

Results (primary -0.2%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.0% [0.7%, 1.6%] 3
Regressions ❌
(secondary)
2.3% [0.6%, 4.7%] 7
Improvements ✅
(primary)
-0.7% [-1.0%, -0.6%] 7
Improvements ✅
(secondary)
-3.0% [-13.2%, -0.4%] 5
All ❌✅ (primary) -0.2% [-1.0%, 1.6%] 10

Binary size

Results (primary 0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.1%] 7
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [0.0%, 0.1%] 7

Bootstrap: 490.618s -> 492.892s (0.46%)
Artifact size: 390.50 MiB -> 390.52 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 1, 2026
@bushrat011899

Copy link
Copy Markdown
Contributor Author

Happy to see no downsides! The "improvement" is only the include-blob bench, just across 5 different compilation modes, so I'm inclined to just call that noise.

@clarfonthey

Copy link
Copy Markdown
Contributor

@bors r+ rollup

Hooray!

@rust-bors

rust-bors Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

📌 Commit a8ef638 has been approved by clarfonthey

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 1, 2026
rust-bors Bot pushed a commit that referenced this pull request Aug 1, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #160262 (Library lock file maintenance)
 - #158548 (Move `std::io::copy` to `alloc::io`)
 - #158814 (Produce an error when `#[inline]` and `#[rust_force_inline]` are used together)
 - #160025 (Fix an edge case with `StepBy::nth` on non-fused iterators)
 - #160271 (Resolver: Introduce `CmRef` which has a speclative borrow variant for `CmRefCell`)
 - #160281 (Fix(lib/fs/tests): Avoid permission denials when cleaning up TempDirs in `set_get_permissions_nofollows*`)
 - #160325 (tidy: Check `proc_macro_deps.rs` by reading it, not by including it)
 - #160334 (Add regression test for unused_allocation on boxed comparison)
@rust-bors
rust-bors Bot merged commit 17acbd8 into rust-lang:main Aug 1, 2026
14 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Aug 1, 2026
rust-timer added a commit that referenced this pull request Aug 1, 2026
Rollup merge of #158548 - bushrat011899:alloc_io_copy_internals, r=clarfonthey

Move `std::io::copy` to `alloc::io`

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`:

```rust
#[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

* No AI tooling of any kind was used during the creation of this PR.
* Please see #154046 (comment) for a review order and broader context for this PR.
jhpratt added a commit to jhpratt/rust that referenced this pull request Aug 1, 2026
…r=clarfonthey

Move `std::io` tests to `alloctests` & add prelude

ACP: rust-lang/libs-team#755
Tracking issue: rust-lang#154046
~~Blocked on: rust-lang#158548~~

## Description

* Moves tests out of `std::io` into `alloctests` now that the relevant items are fully available from `alloc::io`.
* Adds documentation to `alloc::io`
* Adds prelude modules to `core::io` and `alloc::io`.

---

## Notes

* No AI tooling of any kind was used during the creation of this PR.
jhpratt added a commit to jhpratt/rust that referenced this pull request Aug 2, 2026
…r=clarfonthey

Move `std::io` tests to `alloctests` & add prelude

ACP: rust-lang/libs-team#755
Tracking issue: rust-lang#154046
~~Blocked on: rust-lang#158548~~

## Description

* Moves tests out of `std::io` into `alloctests` now that the relevant items are fully available from `alloc::io`.
* Adds documentation to `alloc::io`
* Adds prelude modules to `core::io` and `alloc::io`.

---

## Notes

* No AI tooling of any kind was used during the creation of this PR.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 2, 2026
…r=clarfonthey

Move `std::io` tests to `alloctests` & add prelude

ACP: rust-lang/libs-team#755
Tracking issue: rust-lang#154046
~~Blocked on: rust-lang#158548~~

## Description

* Moves tests out of `std::io` into `alloctests` now that the relevant items are fully available from `alloc::io`.
* Adds documentation to `alloc::io`
* Adds prelude modules to `core::io` and `alloc::io`.

---

## Notes

* No AI tooling of any kind was used during the creation of this PR.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 2, 2026
…r=clarfonthey

Move `std::io` tests to `alloctests` & add prelude

ACP: rust-lang/libs-team#755
Tracking issue: rust-lang#154046
~~Blocked on: rust-lang#158548~~

## Description

* Moves tests out of `std::io` into `alloctests` now that the relevant items are fully available from `alloc::io`.
* Adds documentation to `alloc::io`
* Adds prelude modules to `core::io` and `alloc::io`.

---

## Notes

* No AI tooling of any kind was used during the creation of this PR.
rust-timer added a commit that referenced this pull request Aug 2, 2026
Rollup merge of #156527 - bushrat011899:core_io_test_merge, r=clarfonthey

Move `std::io` tests to `alloctests` & add prelude

ACP: rust-lang/libs-team#755
Tracking issue: #154046
~~Blocked on: #158548~~

## Description

* Moves tests out of `std::io` into `alloctests` now that the relevant items are fully available from `alloc::io`.
* Adds documentation to `alloc::io`
* Adds prelude modules to `core::io` and `alloc::io`.

---

## Notes

* No AI tooling of any kind was used during the creation of this PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

5 participants