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

Remove box syntax from compiler and tools #87781

Merged
merged 11 commits into from
Aug 18, 2021
Merged

Conversation

est31
Copy link
Member

@est31 est31 commented Aug 5, 2021

Removes box syntax from the compiler and tools. In #49733, the future of box syntax is uncertain and the use in the compiler was listed as one of the reasons to keep it. Removal of box syntax might affect the code generated and slow down the compiler so I'd recommend doing a perf run on this.

@rust-highfive
Copy link
Collaborator

r? @oli-obk

(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 Aug 5, 2021
@rust-log-analyzer

This comment has been minimized.

@est31
Copy link
Member Author

est31 commented Aug 5, 2021

@bors try

@bors
Copy link
Contributor

bors commented Aug 5, 2021

⌛ Trying commit 0e42280 with merge 48ad90101547f3344a58a700a4dbcb4590a14e2b...

@jyn514
Copy link
Member

jyn514 commented Aug 5, 2021

#49733 says

how easy is it to remove box syntax as a language feature? Can we just describe it as "will never be stabilised in its current form as it's an internal implementation detail, may be stabilised under a new design of placement")?
is there any reason for users to use box syntax (apart from it looking nice) given Box::new is inline always, or does that not work?
if we decide to keep this, there are a number of unresolved questions in box and in expressions (tracking issue for RFC 809) #22181 about the syntax behaviour

That sounds to me like it's not at all certain that it's going to be removed.

@est31
Copy link
Member Author

est31 commented Aug 5, 2021

@jyn514 good point but note that the issue also contains:

I didn't want to remove it (yet) due to widespread usage in the compiler (Box::new is implemented in terms of it) and in the wider ecosystem (unlike <- etc which only had a handful of crates using it).

This PR alleviates the "widespread usage in the compiler" point, if wanted.

Also note that currently the compiler has inconsistent usage of Box::new vs box. Currently if I grep for Box::new in the compiler/ dir, I get 278 uses. With this PR, there would be 413 uses. So it's 135 uses of box syntax, or 33% of all box constructions. If this PR turns out to have a significant negative performance impact, it won't just influence the decision on whether to keep the box language feature, one could think about introducing the box syntax to more places in the compiler, in the hope of speeding up things.

@jyn514
Copy link
Member

jyn514 commented Aug 5, 2021

@rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 5, 2021
@est31
Copy link
Member Author

est31 commented Aug 5, 2021

(also I have edited the PR description to remove the claim that the tracking issue plans certain removal)

@bors
Copy link
Contributor

bors commented Aug 5, 2021

☀️ Try build successful - checks-actions
Build commit: 48ad90101547f3344a58a700a4dbcb4590a14e2b (48ad90101547f3344a58a700a4dbcb4590a14e2b)

@rust-timer
Copy link
Collaborator

Queued 48ad90101547f3344a58a700a4dbcb4590a14e2b with parent d4ad1cf, future comparison URL.

@hellow554
Copy link
Contributor

One question though: Without the box feature, how will Box::new be implemented in the future? Will it become a language item?

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (48ad90101547f3344a58a700a4dbcb4590a14e2b): comparison url.

Summary: This benchmark run did not return any significant changes.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf -perf-regression

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 5, 2021
@leonardo-m
Copy link

So it's 135 uses of box syntax, or 33% of all box constructions. If this PR turns out to have a significant negative performance impact,

Another benchmark worth doing is to turn all of them into box syntax.

@bjorn3
Copy link
Member

bjorn3 commented Aug 5, 2021

One question though: Without the box feature, how will Box::new be implemented in the future? Will it become a language item?

It could call the allocator without lang item indirection like it currently does (exchange_malloc), use ptr::write to store the value and then construct the Box directly from the pointer. All (try_)new_* methods already need to do this instead of box expr.

@est31 est31 mentioned this pull request Aug 5, 2021
@hellow554
Copy link
Contributor

hellow554 commented Aug 6, 2021

If I see it correctly, you haven't removed every occurence of the box, right?

I really would like to see that. I think it would be easiest to remove the features box_syntax and box_patterns and see where the compiler complains to get rid of everything.

Getting rid of box_patterns seems to be more complicated than intentionally anticipated.

@est31 est31 changed the title Remove box syntax from the compiler Remove box syntax from compiler, library and tools Aug 6, 2021
@jyn514
Copy link
Member

jyn514 commented Aug 6, 2021

Please do not remove box_patterns. It makes code vastly more easy to read and it also has a potential replacement with the Deref pattern project.

@hellow554
Copy link
Contributor

Can we have another timer run?

@Mark-Simulacrum
Copy link
Member

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@Mark-Simulacrum
Copy link
Member

I don't think a straight revert is necessarily the right approach -- the regressions here are tiny, realistically, and wall times do not show a corresponding significant decrease.

Cachegrind diff for serde-doc point to a particular set of functions which likely drive the regressions -- targeted reverts likely make more sense here than a blanket revert. That said, if we revert everything in rustdoc for now that doesn't seem the end of the world either. Ultimately, this just points to Box::new being slightly less able to optimize than box, which was already well known.

@Mark-Simulacrum
Copy link
Member

I also see no particular reason to believe that collect_blanket_impls has been affected by this patch -- can you say more about that perhaps?

@est31
Copy link
Member Author

est31 commented Sep 20, 2021

I've filed a PR in #89134 reverting every change of librustdoc. If anything can stay, please point it out!

the8472 added a commit to the8472/rust that referenced this pull request Sep 22, 2021
…GuillaumeGomez

Revert the rustdoc box syntax removal

Reverts the rustdoc box syntax removal from rust-lang#87781.

It turned out to cause (minor) perf regressions.

Requested in rust-lang#87781 (comment)
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 22, 2021
…illaumeGomez

Revert the rustdoc box syntax removal

Reverts the rustdoc box syntax removal from rust-lang#87781.

It turned out to cause (minor) perf regressions.

Requested in rust-lang#87781 (comment)
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 26, 2021
…crum

Remove most box syntax uses from the testsuite except for src/test/ui/issues

Removes most box syntax uses from the testsuite outside of the src/test/ui/issues directory. The goal was to only change tests where box syntax is an implementation detail instead of the actual feature being tested. So some tests were left out, like the regression test for rust-lang#87935, or tests where the obtained error message changed significantly.

Mostly this replaces box syntax with `Box::new`, but there are some minor drive by improvements, like formatting improvements or `assert_eq` instead of `assert!( == )`.

Prior PR that removed box syntax from the compiler and tools: rust-lang#87781
@safinaskar
Copy link
Contributor

#87781 (comment) :

try_new method is different that it cannot be assumed to be successful so for semantics you'll need to construct the value first and move it later anyway

?????
So, try_new always performs move? This is very bad. Compare this with C++ code:

try {
   a = new some_constructor(some_args);
}catch(...){
  //...
}

Here we can check for allocation errors while directly constructing value in place!

@bjorn3
Copy link
Member

bjorn3 commented Jan 26, 2022

On nightly you can use Box::new_uninit() followed by .assume_init() after writing the value. This is unsafe though. On stable you can use Box::new() of a Box<MaybeUninit<T>> type and transmute to Box<T> after writing the value.

#![feature(new_uninit)]

let mut five = Box::<u32>::new_uninit();

let five: Box<u32> = unsafe {
    // Deferred initialization:
    five.as_mut_ptr().write(5);

    five.assume_init()
};

You can do something similar for the try_* variant.

bors added a commit to rust-lang-ci/rust that referenced this pull request May 22, 2022
Remove box syntax from rustc_mir_dataflow and rustc_mir_transform

Continuation of rust-lang#87781, inspired by rust-lang#97239. The usages that this PR removes have not appeared from nothing, instead the usage in `rustc_mir_dataflow` and `rustc_mir_transform` was from rust-lang#80522 which split up `rustc_mir`, and which was filed before I filed rust-lang#87781, so it was using the state from before my PR. But it was merged after my PR was merged, so the `box_syntax` uses were able to survive here. Outside of this introduction due to the code being outside of the master branch at the point of merging of my PR, there was only one other introduction of box syntax, in rust-lang#95159. That box syntax was removed again though in rust-lang#95555. Outside of that, `box_syntax` has not made its reoccurrance in compiler crates.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 2, 2022
Add #[rustc_box] and use it inside alloc

This commit adds an alternative content boxing syntax, and uses it inside alloc.

```Rust
#![feature(box_syntax)]

fn foo() {
    let foo = box bar;
}
```

is equivalent to

```Rust
#![feature(rustc_attrs)]

fn foo() {
    let foo = #[rustc_box] Box::new(bar);
}
```

The usage inside the very performance relevant code in
liballoc is the only remaining relevant usage of box syntax
in the compiler (outside of tests, which are comparatively easy to port).

box syntax was originally designed to be used by all Rust
developers. This introduces a replacement syntax more tailored
to only being used inside the Rust compiler, and with it,
lays the groundwork for eventually removing box syntax.

[Earlier work](rust-lang#87781 (comment)) by `@nbdd0121` to lower `Box::new` to `box` during THIR -> MIR building ran into borrow checker problems, requiring the lowering to be adjusted in a way that led to [performance regressions](rust-lang#87781 (comment)). The proposed change in this PR lowers `#[rustc_box] Box::new` -> `box` in the AST -> HIR lowering step, which is way earlier in the compiler, and thus should cause less issues both performance wise as well as regarding type inference/borrow checking/etc. Hopefully, future work can move the lowering further back in the compiler, as long as there are no performance regressions.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 13, 2022
Remove most box syntax from librustdoc

This is the second attempt after the librustdoc specific changes have been reverted from rust-lang#87781 in rust-lang#89134, due to a minor, but exant regression caused by the changes. ~~There have been some changes to librustdoc in the past and maybe thanks to them there is no regression any more. If there is still a regression, one can investigate further and maybe find ways to fix the regressions. Thus, i request a perf run.~~ Edit: turns out there is still a regression, but it's caused only by a subset of the changes. So I've changed this PR to only contains the changes that don't cause any performance regressions, keeping the regression causing changes for a later PR.
workingjubilee pushed a commit to tcdi/postgrestd that referenced this pull request Sep 15, 2022
Add #[rustc_box] and use it inside alloc

This commit adds an alternative content boxing syntax, and uses it inside alloc.

```Rust
#![feature(box_syntax)]

fn foo() {
    let foo = box bar;
}
```

is equivalent to

```Rust
#![feature(rustc_attrs)]

fn foo() {
    let foo = #[rustc_box] Box::new(bar);
}
```

The usage inside the very performance relevant code in
liballoc is the only remaining relevant usage of box syntax
in the compiler (outside of tests, which are comparatively easy to port).

box syntax was originally designed to be used by all Rust
developers. This introduces a replacement syntax more tailored
to only being used inside the Rust compiler, and with it,
lays the groundwork for eventually removing box syntax.

[Earlier work](rust-lang/rust#87781 (comment)) by `@nbdd0121` to lower `Box::new` to `box` during THIR -> MIR building ran into borrow checker problems, requiring the lowering to be adjusted in a way that led to [performance regressions](rust-lang/rust#87781 (comment)). The proposed change in this PR lowers `#[rustc_box] Box::new` -> `box` in the AST -> HIR lowering step, which is way earlier in the compiler, and thus should cause less issues both performance wise as well as regarding type inference/borrow checking/etc. Hopefully, future work can move the lowering further back in the compiler, as long as there are no performance regressions.
@est31 est31 mentioned this pull request Feb 26, 2023
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. perf-regression Performance regression. 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.