-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #86701 - sexxi-goose:optimization, r=nikomatsakis
2229: Reduce the size of closures with `capture_disjoint_fields` One key observation while going over the closure size profile of rustc was that we are disjointly capturing one or more fields starting at an immutable reference. Disjoint capture over immutable reference doesn't add too much value because the fields can either be borrowed immutably or copied. One possible edge case of the optimization is when a fields of a struct have a longer lifetime than the structure, therefore we can't completely get rid of all the accesses on top of sharef refs, only the rightmost one. Here is a possible example: ```rust struct MyStruct<'a> { a: &'static A, b: B, c: C<'a>, } fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static { let c = || drop(&*m.a.field_of_a); // Here we really do want to capture `*m.a` because that outlives `'static` // If we capture `m`, then the closure no longer outlives `'static' // it is constrained to `'a` } ``` r? `@nikomatsakis`
- Loading branch information
Showing
8 changed files
with
159 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// edition:2021 | ||
|
||
#![feature(rustc_attrs)] | ||
#![allow(unused)] | ||
#![allow(dead_code)] | ||
|
||
struct Int(i32); | ||
struct B<'a>(&'a i32); | ||
|
||
const I : Int = Int(0); | ||
const REF_I : &'static Int = &I; | ||
|
||
|
||
struct MyStruct<'a> { | ||
a: &'static Int, | ||
b: B<'a>, | ||
} | ||
|
||
fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static { | ||
let c = #[rustc_capture_analysis] || drop(&m.a.0); | ||
//~^ ERROR: attributes on expressions are experimental | ||
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> | ||
//~| ERROR: First Pass analysis includes: | ||
//~| ERROR: Min Capture analysis includes: | ||
//~| NOTE: Capturing m[Deref,(0, 0),Deref] -> ImmBorrow | ||
//~| NOTE: Min Capture m[Deref,(0, 0),Deref] -> ImmBorrow | ||
c | ||
} | ||
|
||
fn main() { | ||
let t = 0; | ||
let s = MyStruct { a: REF_I, b: B(&t) }; | ||
let _ = foo(&s); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
error[E0658]: attributes on expressions are experimental | ||
--> $DIR/edge_case.rs:20:13 | ||
| | ||
LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information | ||
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable | ||
|
||
error: First Pass analysis includes: | ||
--> $DIR/edge_case.rs:20:39 | ||
| | ||
LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
note: Capturing m[Deref,(0, 0),Deref] -> ImmBorrow | ||
--> $DIR/edge_case.rs:20:48 | ||
| | ||
LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ||
| ^^^^^ | ||
|
||
error: Min Capture analysis includes: | ||
--> $DIR/edge_case.rs:20:39 | ||
| | ||
LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
note: Min Capture m[Deref,(0, 0),Deref] -> ImmBorrow | ||
--> $DIR/edge_case.rs:20:48 | ||
| | ||
LL | let c = #[rustc_capture_analysis] || drop(&m.a.0); | ||
| ^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
27 changes: 27 additions & 0 deletions
27
src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// edition:2021 | ||
// run-pass | ||
|
||
#![allow(unused)] | ||
#![allow(dead_code)] | ||
|
||
struct Int(i32); | ||
struct B<'a>(&'a i32); | ||
|
||
const I : Int = Int(0); | ||
const REF_I : &'static Int = &I; | ||
|
||
struct MyStruct<'a> { | ||
a: &'static Int, | ||
b: B<'a>, | ||
} | ||
|
||
fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static { | ||
let c = || drop(&m.a.0); | ||
c | ||
} | ||
|
||
fn main() { | ||
let t = 0; | ||
let s = MyStruct { a: REF_I, b: B(&t) }; | ||
let _ = foo(&s); | ||
} |