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 7 pull requests #118126

Merged
merged 24 commits into from
Nov 21, 2023
Merged

Rollup of 7 pull requests #118126

merged 24 commits into from
Nov 21, 2023

Conversation

Noratrieb
Copy link
Member

Successful merges:

r? @ghost
@rustbot modify labels: rollup

Create a similar rollup

rcvalle and others added 24 commits November 10, 2023 08:20
Adds missing use core::ffi::c_int for when
sanitizer_cfi_normalize_integers is defined.
Some UI tests trigger behavior in rustc where it reads $CARGO and changes behavior if it exists.
To make the tests work that rely on it not being set, make sure it is not set.

By default, this is not set, but people may do weird hacks
that cause it to be set.
While a better approach would be to implement it for all ZSTs
which are `Copy` and have trivial `Clone`,
the last property cannot be detected for now.

Signed-off-by: Petr Portnov <me@progrm-jarvis.ru>
Signed-off-by: Petr Portnov <me@progrm-jarvis.ru>
Signed-off-by: Petr Portnov <me@progrm-jarvis.ru>
And remove `pub` from some local-only ones.
The majority of these aren't actually used, but I kept them anyway.
…kingjubilee

CFI: Add missing use core::ffi::c_int

Adds missing use core::ffi::c_int for when sanitizer_cfi_normalize_integers is defined.
Explicitly unset $CARGO for compiletest

Some UI tests trigger behavior in rustc where it reads $CARGO and changes behavior if it exists. To make the tests work that rely on it not being set, make sure it is not set.

By default, this is not set, but people may do weird hacks that cause it to be set.

closes rust-lang#118058
…iler-errors

`rustc_ty_utils` cleanups

Minor improvements I found while looking at this code.

r? ``@lcnr``
…-tuple, r=thomcc

feat: specialize `SpecFromElem` for `()`

# Description

This PR adds a specialization `SpecFromElem for ()` which allows to significantly reduce `vec![(), N]` time in debug builds (specifically, tests) turning it from observable $O(n)$ to $O(1)$.

# Observing the change

The problem this PR aims to fix explicitly is slow `vec![(), N]` on big `N`s which may appear in tests (see [Background section](#Background) for more details).

The minimal example to see the problem:

```rust
#![feature(test)]

extern crate test;

#[cfg(test)]
mod tests {
    const HUGE_SIZE: usize = i32::MAX as usize + 1;

    #[bench]
    fn bench_vec_literal(b: &mut test::Bencher) {
        b.iter(|| vec![(); HUGE_SIZE]);
    }

    #[bench]
    fn bench_vec_repeat(b: &mut test::Bencher) {
        b.iter(|| [(); 1].repeat(HUGE_SIZE));
    }
}
```
<details><summary>Output</summary>
<p>

```bash
cargo +nightly test -- --report-time -Zunstable-options
   Compiling huge-zst-vec-literal-bench v0.1.0 (/home/progrm_jarvis/RustroverProjects/huge-zst-vec-literal-bench)
    Finished test [unoptimized + debuginfo] target(s) in 0.31s
     Running unittests src/lib.rs (target/debug/deps/huge_zst_vec_literal_bench-e43b1ef287ba8b36)

running 2 tests
test tests::bench_vec_repeat  ... ok <0.000s>
test tests::bench_vec_literal ... ok <14.382s>

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 14.38s

   Doc-tests huge-zst-vec-literal-bench

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
```
</p>
</details>

> [!IMPORTANT]
> This problem is only observable in Debug (unoptimized) builds, while Release (optimized) ones do not observe this problem. It still is worth fixing it, IMO, since the original scenario observes the problem in tests for which optimizations are disabled by default and it seems unreasonable to override this for the whole project while the problem is very local.

# Background

While working on a crate for a custom data format which has an `i32::MAX` limit on its list's sizes, I wrote the following test to ensure that this invariant is upheld:

```rust
#[test]
fn lists_should_have_i32_size() {
    assert!(
        RawNbtList::try_from(vec![(); i32::MAX as usize]).is_ok(),
        "lists should permit the size up to {}",
        i32::MAX
    );
    assert!(
        RawNbtList::try_from(vec![(); i32::MAX as usize + 1]).is_err(),
        "lists should have the size of at most {}",
        i32::MAX
    );
}
```

Soon I discovered that this takes $\approx 3--4s$ per assertion on my machine, almost all of which is spent on `vec![..]`.
While this would be logical for a non-ZST vector (which would require actual $O(n)$ allocation), here `()` was used intentionally considering that for ZSTs size-changing operations should anyway be $O(1)$ (at least from allocator perspective). Yet, this "overhead" is logical if we consider that in general case `clone()` (which is used by `Vec` literal) may have a non-trivial implementation and thus each element has to actually be visited (even if they occupy no space).

In my specific case, the following (contextual) equivalent solved the issue:

```rust
#[test]
fn lists_should_have_i32_size() {
    assert!(
        RawNbtList::try_from([(); 1].repeat(i32::MAX as usize)).is_ok(),
        "lists should permit the size up to {}",
        i32::MAX
    );
    assert!(
        RawNbtList::try_from([(); 1].repeat(i32::MAX as usize + 1)).is_err(),
        "lists should have the size of at most {}",
        i32::MAX
    );
}
```

which works since `repeat` explicitly uses `T: Copy` and so does not have to think about non-trivial `Clone`.

But it still may be counter-intuitive for users to observe such long time on the "canonical" vec literal thus the PR.

# Generic solution

This change is explicitly non-generic. Initially I considered it possible to implement in generically, but this would require the specialization to have the following type requirements:
- ✅ the type must be a ZST: easily done via
  ```rust
  if core::mem::size_of::<T>() == 0 {
    todo!("specialization")
  }
  ```
  or
  ```rust
  use core::mem::SizedTypeProperties;
  if T::IS_ZST {
    todo!("specialization")
  }
  ```
- :white_check_mark`: the type must implement `Copy`: implementable non-conflictable via a separate specialization:
  ```rust
  trait IsCopyZst: Sized {
    fn is_copy_zst() -> bool;
  }
  impl<T> IsCopyZst for T {
    fn is_copy_zst() -> bool {
        false
    }
  }
  impl<T: Copy> IsCopyZst for T {
    fn is_copy_zst() -> bool {
        Self::IS_ZST
    }
  }
  ```
- ❌ the type should have a trivial `Clone` implementation: since `vec![t; n]` is specified to use `clone()`, we can only use this "performance optimization" when we are guaranteed that `clone()` does nothing except for copying.

The latter is the real blocker for a generic fix since I am unaware of any way to get this information in a compiler-guaranteed way.

While there may be a fix for this (my friend `@CertainLach` has suggested a potential solution by an perma-unstable fn in `Clone` like `is_trivially_cloneable()` defaulting to `false` and only overridable by `rustc` on derive), this is surely out of this PRs scope.
Update books

## rust-lang/book

2 commits in 5b6c1ceaa62ecbd6caef08df39b33b3938e99deb..71352deb20727b4dda9ebfe8182709d5bf17dfea
2023-11-09 14:49:45 UTC to 2023-11-09 14:49:16 UTC

- Fixed 'Devtools' link (rust-lang/book#3770)
- Fix mdBook links (rust-lang/book#3769)

## rust-lang/rust-by-example

7 commits in 311b84962016b28c75525c86e7b3f49fd9101a39..a6581246f96837113968c02187db24f742af3908
2023-11-18 21:45:20 UTC to 2023-11-07 22:32:53 UTC

- rename `y` to `_y` to get the correct compile error (rust-lang/rust-by-example#1769)
- fix test name in cargo/test.md (rust-lang/rust-by-example#1768)
- Various minor edits for typo fixes, formatting fixes, and clarifications (rust-lang/rust-by-example#1765)
- Update closures.md to correct a typo (rust-lang/rust-by-example#1763)
- Link to the Bulgarian translation (rust-lang/rust-by-example#1764)
- Fix asm example explanation for `inlateout` usage (22.1 Inline Assembly) (rust-lang/rust-by-example#1766)
- Update index.md: Added descriptions for the 'leftover' points (rust-lang/rust-by-example#1767)

## rust-lang/rustc-dev-guide

3 commits in 77dbe5782b2488af3bb489ad702eaff438f465bf..ddb8b1309f9e905804cea1e248a4572fed6b464b
2023-11-18 21:08:13 UTC to 2023-11-08 14:43:50 UTC

- Add link for unsize.md (rust-lang/rustc-dev-guide#1825)
- Fix typo in contribution walkthrough (rust-lang/rustc-dev-guide#1824)
- Update documentation for coverage tests (rust-lang/rustc-dev-guide#1823)
…piler-errors

Fix occurrences of old fn names in comment and tracing
…errors

`rustc_hir` cleanups

Just some improvements I found while looking at this code.

r? `@WaffleLapkin`
@rustbot rustbot added O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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. rollup A PR which is a rollup labels Nov 21, 2023
@Noratrieb
Copy link
Member Author

@bors r+ rollup=never p=7

@bors
Copy link
Contributor

bors commented Nov 21, 2023

📌 Commit f13f980 has been approved by Nilstrieb

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 Nov 21, 2023
@bors
Copy link
Contributor

bors commented Nov 21, 2023

⌛ Testing commit f13f980 with merge c5af061...

@bors
Copy link
Contributor

bors commented Nov 21, 2023

☀️ Test successful - checks-actions
Approved by: Nilstrieb
Pushing c5af061 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 21, 2023
@bors bors merged commit c5af061 into rust-lang:master Nov 21, 2023
12 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Nov 21, 2023
@rust-timer
Copy link
Collaborator

📌 Perf builds for each rolled up PR:

PR# Message Perf Build Sha
#117790 CFI: Add missing use core::ffi::c_int 2762f76e78e2e8a6396a79042b42aca9651da6db (link)
#118059 Explicitly unset $CARGO for compiletest 0130abd7c03bdb3e30f12d52a01feb6e30e4311d (link)
#118081 rustc_ty_utils cleanups 6f9030116291308b534ed28ca36c55563ca64800 (link)
#118094 feat: specialize SpecFromElem for () 7ab152aea1f5d3325f7d5c1a9d69b8c447038dcf (link)
#118097 Update books da8ad0a9582f09f19fd07622b63661ccd949eb7a (link)
#118115 Fix occurrences of old fn names in comment and tracing 07dc609ab4c6ef759158eb8b8e14397102d33e3a (link)
#118121 rustc_hir cleanups 0927ca88043d3b0e3a51f31c763bff15a42817a9 (link)

previous master: 85c42b751e

In the case of a perf regression, run the following command for each PR you suspect might be the cause: @rust-timer build $SHA

@Noratrieb Noratrieb deleted the rollup-5ogh896 branch November 21, 2023 12:01
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c5af061): comparison URL.

Overall result: ✅ improvements - 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
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

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)
- - 0
Regressions ❌
(secondary)
2.7% [1.5%, 4.2%] 8
Improvements ✅
(primary)
-3.2% [-3.2%, -3.2%] 1
Improvements ✅
(secondary)
-2.1% [-2.9%, -0.7%] 5
All ❌✅ (primary) -3.2% [-3.2%, -3.2%] 1

Cycles

Results

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)
- - 0
Regressions ❌
(secondary)
2.3% [2.2%, 2.4%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

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

Bootstrap: 675.345s -> 674.399s (-0.14%)
Artifact size: 313.77 MiB -> 313.75 MiB (-0.01%)

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. O-unix Operating system: Unix-like rollup A PR which is a rollup S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants