Skip to content
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

Add slice::fill #70752

Merged
merged 1 commit into from Apr 5, 2020
Merged

Add slice::fill #70752

merged 1 commit into from Apr 5, 2020

Conversation

yoshuawuyts
Copy link
Member

@yoshuawuyts yoshuawuyts commented Apr 3, 2020

Adds the slice::fill method to fill a slice with an item. This replaces manual for loops where items are copied one-by-one. This is a counterpart to C++20's std::fill function.

Usage

let mut buf = vec![0; 10];
buf.fill(1);
assert_eq!(buf, vec![1; 10]);

Performance

When compiling in release mode, for [u8] and [u16] this method will optimize to a memset(3) call (godbolt). The initial implementation relies on LLVM's optimizer to make it as fast as possible for any given input. But as @jonas-schievink pointed out this can later be optimized through specialization to guarantee it has a specific performance profile.

Why now?

Conversations about adding slice::fill are not new. In fact, rust-lang/rfcs#2067 was opened 3 years ago about this exact topic. However discussion stranded while discussing implementation details, and it's not seen much forward motion since.

In "The Hunt for the Fastest Zero" Travis Downs provides disects C++'s std::fill performance profile on gcc, comparing it among others to memset(3). Even though memset(3) outperforms std::fill in their tests, the author notes the following:

That the optimization fails, perhaps unexpectedly, in some cases is unfortunate but it’s nice that you can fix it yourself. [...] Do we throw out modern C++ idioms, at least where performance matters, for example by replacing std::fill with memset? I don’t think so.

Much of the article focuses on how how to fix the performance of std::fill by providing specializations for specific input. In Rust we don't have any dedicated methods to fill slices with values, so it either needs to be optimized at the MIR layer, or more likely rely on LLVM's optimizer.

By adding a dedicated method for filling slices with values it opens up the ability for us to in the future guarantee that e.g. Vec<u8> will always optimize to memset even in debug mode. Or perhaps provide stronger guarantees about memory when zeroing values when a certain flag is passed. But regardless of that, it improves general ergonomics of working with slices by providing a dedicated method with documentation and examples.

References

@rust-highfive
Copy link
Collaborator

rust-highfive commented Apr 3, 2020

r? @withoutboats

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 3, 2020
@rust-highfive
Copy link
Collaborator

rust-highfive commented Apr 3, 2020

The job mingw-check of your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2020-04-03T22:05:55.7408794Z ========================== Starting Command Output ===========================
2020-04-03T22:05:55.7411200Z [command]/bin/bash --noprofile --norc /home/vsts/work/_temp/2e396c19-8908-4474-be19-9a626761bb5c.sh
2020-04-03T22:05:55.7411480Z 
2020-04-03T22:05:55.7415203Z ##[section]Finishing: Disable git automatic line ending conversion
2020-04-03T22:05:55.7433368Z ##[section]Starting: Checkout rust-lang/rust@refs/pull/70752/merge to s
2020-04-03T22:05:55.7436492Z Task         : Get sources
2020-04-03T22:05:55.7436779Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
2020-04-03T22:05:55.7437058Z Version      : 1.0.0
2020-04-03T22:05:55.7437264Z Author       : Microsoft
---
2020-04-03T22:05:57.0448280Z ##[command]git remote add origin https://github.com/rust-lang/rust
2020-04-03T22:05:57.0455238Z ##[command]git config gc.auto 0
2020-04-03T22:05:57.0460552Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2020-04-03T22:05:57.0466259Z ##[command]git config --get-all http.proxy
2020-04-03T22:05:57.0475675Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/70752/merge:refs/remotes/pull/70752/merge
---
2020-04-03T22:08:30.9962678Z  ---> 3fc1b512c57b
2020-04-03T22:08:30.9966434Z Step 6/7 : ENV RUN_CHECK_WITH_PARALLEL_QUERIES 1
2020-04-03T22:08:30.9973156Z  ---> Using cache
2020-04-03T22:08:30.9973486Z  ---> 5ee4295733f4
2020-04-03T22:08:30.9974715Z Step 7/7 : ENV SCRIPT python2.7 ../x.py test src/tools/expand-yaml-anchors &&            python2.7 ../x.py check --target=i686-pc-windows-gnu --host=i686-pc-windows-gnu &&            python2.7 ../x.py build --stage 0 src/tools/build-manifest &&            python2.7 ../x.py test --stage 0 src/tools/compiletest &&            python2.7 ../x.py test src/tools/tidy &&            /scripts/validate-toolstate.sh
2020-04-03T22:08:30.9981585Z  ---> 3d07a0fa42fe
2020-04-03T22:08:31.0014006Z Successfully built 3d07a0fa42fe
2020-04-03T22:08:31.0111611Z Successfully tagged rust-ci:latest
2020-04-03T22:08:31.0384809Z Built container sha256:3d07a0fa42feb5754fc13bb2f7010ebe13e4b8b8cdbebed0c75d8da320c8c8ad
2020-04-03T22:08:31.0384809Z Built container sha256:3d07a0fa42feb5754fc13bb2f7010ebe13e4b8b8cdbebed0c75d8da320c8c8ad
2020-04-03T22:08:31.0400323Z Looks like docker image is the same as before, not uploading
2020-04-03T22:08:38.7134142Z [CI_JOB_NAME=mingw-check]
2020-04-03T22:08:38.7449278Z [CI_JOB_NAME=mingw-check]
2020-04-03T22:08:38.7513549Z == clock drift check ==
2020-04-03T22:08:38.7513860Z   local time: Fri Apr  3 22:08:38 UTC 2020
2020-04-03T22:08:38.9196505Z   network time: Fri, 03 Apr 2020 22:08:38 GMT
2020-04-03T22:08:38.9227181Z Starting sccache server...
2020-04-03T22:08:39.0040160Z configure: processing command line
2020-04-03T22:08:39.0040796Z configure: 
2020-04-03T22:08:39.0041857Z configure: rust.parallel-compiler := True
---
2020-04-03T22:10:01.1602409Z     Checking core v0.0.0 (/checkout/src/libcore)
2020-04-03T22:10:07.6333596Z error[E0599]: no method named `clone` found for type parameter `T` in the current scope
2020-04-03T22:10:07.6334771Z     --> src/libcore/slice/mod.rs:2160:25
2020-04-03T22:10:07.6335519Z      |
2020-04-03T22:10:07.6336182Z 2160 |             *el = value.clone();
2020-04-03T22:10:07.6337006Z      |                         ^^^^^ method not found in `T`
2020-04-03T22:10:07.6338445Z      = help: items from traits can only be used if the type parameter is bounded by the trait
2020-04-03T22:10:07.6339371Z help: the following trait defines an item `clone`, perhaps you need to restrict type parameter `T` with it:
2020-04-03T22:10:07.6340078Z      |
2020-04-03T22:10:07.6340078Z      |
2020-04-03T22:10:07.6340733Z 58   | impl<T: clone::Clone> [T] {
2020-04-03T22:10:07.6341900Z 
2020-04-03T22:10:08.0321187Z    Compiling libc v0.2.66
2020-04-03T22:10:09.3649781Z    Compiling autocfg v0.1.7
2020-04-03T22:10:09.6400574Z error: aborting due to previous error
---
2020-04-03T22:10:10.3895577Z expected success, got: exit code: 101
2020-04-03T22:10:10.3907433Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap check
2020-04-03T22:10:10.3907732Z Build completed unsuccessfully in 0:01:31
2020-04-03T22:10:10.3956643Z == clock drift check ==
2020-04-03T22:10:11.7515758Z   local time: Fri Apr  3 22:10:11 UTC 2020
2020-04-03T22:10:11.8976923Z   network time: Fri, 03 Apr 2020 22:10:11 GMT
2020-04-03T22:10:14.0214536Z 
2020-04-03T22:10:14.0214536Z 
2020-04-03T22:10:14.0275262Z ##[error]Bash exited with code '1'.
2020-04-03T22:10:14.0352480Z ##[section]Finishing: Run build
2020-04-03T22:10:14.0390594Z ##[section]Starting: Checkout rust-lang/rust@refs/pull/70752/merge to s
2020-04-03T22:10:14.0397548Z Task         : Get sources
2020-04-03T22:10:14.0398175Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.
2020-04-03T22:10:14.0398735Z Version      : 1.0.0
2020-04-03T22:10:14.0399124Z Author       : Microsoft
2020-04-03T22:10:14.0399124Z Author       : Microsoft
2020-04-03T22:10:14.0399758Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkId=798199)
2020-04-03T22:10:14.0400469Z ==============================================================================
2020-04-03T22:10:14.3335064Z Cleaning any cached credential from repository: rust-lang/rust (GitHub)
2020-04-03T22:10:14.3375510Z ##[section]Finishing: Checkout rust-lang/rust@refs/pull/70752/merge to s
2020-04-03T22:10:14.3457814Z Cleaning up task key
2020-04-03T22:10:14.3458990Z Start cleaning up orphan processes.
2020-04-03T22:10:14.3642870Z Terminate orphan process: pid (5095) (python)
2020-04-03T22:10:14.3789497Z ##[section]Finishing: Finalize Job

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @rust-lang/infra. (Feature Requests)

Copy link
Contributor

@Centril Centril left a comment

cc @rust-lang/libs

This seems like a reasonable abstraction to add as outlined by @bluss in rust-lang/rfcs#2067 (comment) towards which there didn't seem to be much disagreement at the time.

There are some small adjustments that are necessary (and you'll need to run ./x.py fmt locally as well, please squash those commits into the first one), but this looks good overall.

As this only adds an unstable method, r=me with these comments addressed.

src/libcore/slice/mod.rs Outdated Show resolved Hide resolved
src/libcore/slice/mod.rs Outdated Show resolved Hide resolved
@Centril
Copy link
Contributor

Centril commented Apr 4, 2020

r? @Centril

@rust-highfive rust-highfive assigned Centril and unassigned withoutboats Apr 4, 2020
@Centril Centril 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 Apr 4, 2020
@yoshuawuyts
Copy link
Member Author

yoshuawuyts commented Apr 4, 2020

@Centril addressed the comments and opened a tracking issue in #70758.

@sfackler
Copy link
Member

sfackler commented Apr 4, 2020

Should this take the fill value by reference since it's cloning it into each position? Alternatively it could tweak the logic a bit to move the value into the last position, but that might just mess it up optimization wise.

@yoshuawuyts
Copy link
Member Author

yoshuawuyts commented Apr 4, 2020

@sfackler oh I like your first suggestion a lot. I just tested this using the Borrow trait and the optimizations remain in place (godbolt). This allows the following to work:

pub fn run_u8(buf: &mut Vec<u8>) {
    buf.fill(&1);
    buf.fill(1);
}

I think that should guarantee the value doesn't need to be consumed, but can also just be passed if desired. I'll go ahead and update the patch to include the Borrow bound.

@Centril
Copy link
Contributor

Centril commented Apr 4, 2020

Thanks; seems @sfackler had some more input so I'll reassign to them. :)

r? @sfackler

@rust-highfive rust-highfive assigned sfackler and unassigned Centril Apr 4, 2020
@Centril Centril 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 Apr 4, 2020
@sfackler
Copy link
Member

sfackler commented Apr 4, 2020

@bors r+ rollup

Thanks!

@bors
Copy link
Contributor

bors commented Apr 4, 2020

📌 Commit 2af9adc has been approved by sfackler

@bors
Copy link
Contributor

bors commented Apr 4, 2020

🌲 The tree is currently closed for pull requests below priority 1000, this pull request will be tested once the tree is reopened

@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 Apr 4, 2020
src/libcore/slice/mod.rs Outdated Show resolved Hide resolved
@yoshuawuyts
Copy link
Member Author

yoshuawuyts commented Apr 4, 2020

Updated with the suggestion from @ollie27 in #70752 (comment) which may reduce resource usage in certain cases.

@dtolnay
Copy link
Member

dtolnay commented Apr 4, 2020

@bors r+

@bors
Copy link
Contributor

bors commented Apr 4, 2020

📌 Commit edabceb has been approved by dtolnay

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 5, 2020
Rollup of 6 pull requests

Successful merges:

 - rust-lang#70635 (rustc_target: Some cleanup to `no_default_libraries`)
 - rust-lang#70748 (Do not disable field reordering on enums with big discriminant)
 - rust-lang#70752 (Add slice::fill)
 - rust-lang#70766 (use ManuallyDrop instead of forget inside collections)
 - rust-lang#70768 (macro_rules: `NtLifetime` cannot start with an identifier)
 - rust-lang#70783 (comment refers to removed type)

Failed merges:

r? @ghost
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 5, 2020
Rollup of 6 pull requests

Successful merges:

 - rust-lang#70635 (rustc_target: Some cleanup to `no_default_libraries`)
 - rust-lang#70748 (Do not disable field reordering on enums with big discriminant)
 - rust-lang#70752 (Add slice::fill)
 - rust-lang#70766 (use ManuallyDrop instead of forget inside collections)
 - rust-lang#70768 (macro_rules: `NtLifetime` cannot start with an identifier)
 - rust-lang#70783 (comment refers to removed type)

Failed merges:

r? @ghost
@bors bors merged commit 6ea2701 into rust-lang:master Apr 5, 2020
@yoshuawuyts yoshuawuyts deleted the slice_fill branch Apr 5, 2020
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants