Skip to content

Implement array::repeat #119127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 19, 2024
Merged

Implement array::repeat #119127

merged 6 commits into from
Jun 19, 2024

Conversation

joboet
Copy link
Member

@joboet joboet commented Dec 19, 2023

See rust-lang/libs-team#310.

I've decided to make the function use the input value as last element instead of cloning it to every position and dropping it, and to make this part of the API so that callers are not surprised by this behaviour.

TODO: open a tracking issue. I'll wait for the ACP to be accepted, first.

@rustbot label +T-libs-api +T-libs
r? libs

@rustbot rustbot added 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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 19, 2023
@scottmcm
Copy link
Member

I've decided to make the function use the input value as last element instead of cloning it to every position and dropping it

As I wrote in the ACP, the way to do this is to use https://stdrs.dev/nightly/x86_64-unknown-linux-gnu/std/iter/fn.repeat_n.html:

from_trusted_iterator(iter::repeat_n(x, N))

No need to MaybeUninit manually here.

@scottmcm scottmcm assigned scottmcm and unassigned m-ou-se Dec 19, 2023
@joboet
Copy link
Member Author

joboet commented Dec 19, 2023

I've decided to make the function use the input value as last element instead of cloning it to every position and dropping it

As I wrote in the ACP, the way to do this is to use https://stdrs.dev/nightly/x86_64-unknown-linux-gnu/std/iter/fn.repeat_n.html:

from_trusted_iterator(iter::repeat_n(x, N))

No need to MaybeUninit manually here.

I fear that would lead to suboptimal codegen, since the optimizer has to figure out that counting backwards results in the same amount of iterations as iterating forwards, which in my experience it is not really able to do well.

@scottmcm
Copy link
Member

You should use the direct implementation unless you have a codegen test demonstrating that the custom implementation is actually better. Counting down can actually be better than counting up, in assembly (example).

Regardless of the implementation, it might be good to have some simple tests like checking that repeat(123_u8) will optimize to a single memset. It looks like that'll happen for the iter::repeat_n approach, at least: https://rust.godbolt.org/z/EhG1azc67.

@rust-log-analyzer

This comment has been minimized.

@scottmcm scottmcm added S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2024
@scottmcm
Copy link
Member

Finally approved in rust-lang/libs-team#310 (comment) !

I'm going to re-run CI as a double-check, since it's been quite a while.

@scottmcm scottmcm closed this Jun 18, 2024
@scottmcm scottmcm reopened this Jun 18, 2024
@scottmcm scottmcm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. labels Jun 18, 2024
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 19, 2024
@joboet
Copy link
Member Author

joboet commented Jun 19, 2024

Rebased, updated and tracking issue added.
@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 19, 2024
@jieyouxu jieyouxu added the F-array_repeat `#![feature(array_repeat)]` label Jun 19, 2024
@scottmcm
Copy link
Member

Thanks! Excited to finally get this 🙂

@bors r+ rollup=iffy (new codegen test, which isn't tested in CI)

@bors
Copy link
Collaborator

bors commented Jun 19, 2024

📌 Commit 0aa3310 has been approved by scottmcm

It is now in the queue for this repository.

@bors bors 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 Jun 19, 2024
@bors
Copy link
Collaborator

bors commented Jun 19, 2024

⌛ Testing commit 0aa3310 with merge d8a38b0...

@bors
Copy link
Collaborator

bors commented Jun 19, 2024

☀️ Test successful - checks-actions
Approved by: scottmcm
Pushing d8a38b0 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 19, 2024
@bors bors merged commit d8a38b0 into rust-lang:master Jun 19, 2024
7 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jun 19, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d8a38b0): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

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

Max RSS (memory usage)

Results (primary 3.1%, secondary -3.3%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
3.1% [2.5%, 3.8%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-4.0%, -2.5%] 2
All ❌✅ (primary) 3.1% [2.5%, 3.8%] 2

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 691.194s -> 692.485s (0.19%)
Artifact size: 323.71 MiB -> 323.83 MiB (0.04%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-array_repeat `#![feature(array_repeat)]` merged-by-bors This PR was explicitly merged by bors. 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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants