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

Rollup of 6 pull requests #70800

Merged
merged 12 commits into from
Apr 5, 2020
Merged

Rollup of 6 pull requests #70800

merged 12 commits into from
Apr 5, 2020

Conversation

Dylan-DPC-zz
Copy link

Successful merges:

Failed merges:

r? @ghost

ogoffart and others added 12 commits April 3, 2020 23:12
The field are always re-ordered to minimize padding, regardless of the
alignment of the discriminant
This commit changes some usage of mem::forget into mem::ManuallyDrop
in some Vec, VecDeque, BTreeMap and Box methods.

Before the commit, the generated IR for some of the methods was
longer, and even after optimization, some unwinding artifacts were
still present.
rustc_target: Some cleanup to `no_default_libraries`
Do not disable field reordering on enums with big discriminant

The field are always re-ordered to minimize padding, regardless of the
alignment of the discriminant

(spinoff from rust-lang#70477)
Add slice::fill

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`](https://en.cppreference.com/w/cpp/algorithm/fill) function.

## Usage

```rust
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](https://godbolt.org/z/85El_c)). 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](https://twitter.com/sheevink/status/1245756597453885442) 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"](https://travisdowns.github.io/blog/2020/01/20/zero.html) 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
- [slice-fill prototype on docs.rs](https://docs.rs/slice-fill/1.0.1/slice_fill/)
- [The Hunt For The Fastest Zero](https://travisdowns.github.io/blog/2020/01/20/zero.html)
- [Safe memset for slices](rust-lang/rfcs#2067)
- [C++20 std::fill](https://en.cppreference.com/w/cpp/algorithm/fill)
- [ASM output on Godbolt](https://godbolt.org/z/5-XU66)
…Mark-Simulacrum,RalfJung

use ManuallyDrop instead of forget inside collections

This PR changes some usage of `mem::forget` into `mem::ManuallyDrop` in some `Vec`, `VecDeque`, `BTreeMap` and `Box` methods.

Before the commit, the generated IR for some of the methods was longer, and even after optimization, some unwinding artifacts were still present.
…rk-i-m

macro_rules: `NtLifetime` cannot start with an identifier

Fixes rust-lang#70446
comment refers to removed type

Was removed in 51938c6
@Dylan-DPC-zz
Copy link
Author

@bors r+ rollup=never p=6

@bors
Copy link
Contributor

bors commented Apr 5, 2020

📌 Commit 5c42faf has been approved by Dylan-DPC

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Apr 5, 2020
@bors
Copy link
Contributor

bors commented Apr 5, 2020

⌛ Testing commit 5c42faf with merge c0ee793d85cb4c18a9df01307f632a478385a9d7...

@bors
Copy link
Contributor

bors commented Apr 5, 2020

💔 Test failed - checks-azure

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 5, 2020
@Dylan-DPC-zz
Copy link
Author

@bors retry

@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 5, 2020
@bors
Copy link
Contributor

bors commented Apr 5, 2020

⌛ Testing commit 5c42faf with merge 7b657d3...

@bors
Copy link
Contributor

bors commented Apr 5, 2020

☀️ Test successful - checks-azure
Approved by: Dylan-DPC
Pushing 7b657d3 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 5, 2020
@bors bors merged commit 7b657d3 into rust-lang:master Apr 5, 2020
@rust-highfive
Copy link
Collaborator

📣 Toolstate changed by #70800!

Tested on commit 7b657d3.
Direct link to PR: #70800

💔 rls on linux: test-pass → test-fail (cc @Xanewok).

rust-highfive added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Apr 5, 2020
Tested on commit rust-lang/rust@7b657d3.
Direct link to PR: <rust-lang/rust#70800>

💔 rls on linux: test-pass → test-fail (cc @Xanewok).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants