Skip to content

Implement array::repeat#119127

Merged
bors merged 6 commits into
rust-lang:masterfrom
joboet:array_repeat
Jun 19, 2024
Merged

Implement array::repeat#119127
bors merged 6 commits into
rust-lang:masterfrom
joboet:array_repeat

Conversation

@joboet

@joboet joboet commented Dec 19, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Comment thread library/core/src/array/mod.rs
Comment thread library/core/src/array/mod.rs Outdated
@joboet

joboet commented Dec 19, 2023

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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.

Comment thread tests/codegen/array-repeat.rs Outdated
@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 joboet mentioned this pull request Jun 19, 2024
4 tasks
@joboet

joboet commented Jun 19, 2024

Copy link
Copy Markdown
Member Author

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
Copy Markdown
Member

Thanks! Excited to finally get this 🙂

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

@bors

bors commented Jun 19, 2024

Copy link
Copy Markdown
Collaborator

📌 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

bors commented Jun 19, 2024

Copy link
Copy Markdown
Collaborator

⌛ Testing commit 0aa3310 with merge d8a38b0...

@bors

bors commented Jun 19, 2024

Copy link
Copy Markdown
Collaborator

☀️ 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
@rustbot rustbot added this to the 1.81.0 milestone Jun 19, 2024
@rust-timer

Copy link
Copy Markdown
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