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 separate array length function #86404

Closed
wants to merge 2 commits into from
Closed

Conversation

shamatar
Copy link
Contributor

Solves #86403

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @scottmcm (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 17, 2021
@jonas-schievink
Copy link
Contributor

This is a breaking change, because the method is unstable

@shamatar
Copy link
Contributor Author

shamatar commented Jun 17, 2021

If it's accepted as insta-stable then it is actually not breaking. I can make it this way for CI purposes

@scottmcm scottmcm added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jun 17, 2021
@scottmcm
Copy link
Member

Hmm, is this one of the places where the "avoid unstable methods in name resolution" fix kicks in?

Maybe add a ui test, @shamatar, to show what people not using the feature will get, to find out?

@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member

Ah, those CI results are enough to say that this is definitely breaking.

I'd suggest making a thread in #t-libs on zulip to ask libs how they feel about doing this.

@scottmcm scottmcm 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 Jun 17, 2021
@shamatar
Copy link
Contributor Author

I'm very surprised that unstable method resolution didn't work with it.

So far I'd still argue to mark it as stable and make a crater run with it, but let me ask on Zullip

@shamatar
Copy link
Contributor Author

Duplicating from Zullip

I've made a simple MIR optimization test for a code like

// EMIT_MIR lower_slice_len.array_bound.SimplifyLocals.diff
pub fn array_bound<const N: usize>(index: usize, slice: &[u8; N]) -> u8 {
    if index < slice.len() {
        slice[index]
    } else {
        42
    }
}

(hacking into SimplifyLocals as the one of the last passes for simplicity, so after any constant prop, and mir-opt-level = 3)

here is a MIR

- // MIR for `array_bound` before SimplifyLocals
+ // MIR for `array_bound` after SimplifyLocals
  
  fn array_bound(_1: usize, _2: &[u8; N]) -> u8 {
      debug index => _1;                   // in scope 0 at $DIR/lower_slice_len.rs:14:36: 14:41
      debug slice => _2;                   // in scope 0 at $DIR/lower_slice_len.rs:14:50: 14:55
      let mut _0: u8;                      // return place in scope 0 at $DIR/lower_slice_len.rs:14:70: 14:72
      let mut _3: bool;                    // in scope 0 at $DIR/lower_slice_len.rs:15:8: 15:27
      let mut _4: usize;                   // in scope 0 at $DIR/lower_slice_len.rs:15:8: 15:13
      let mut _5: usize;                   // in scope 0 at $DIR/lower_slice_len.rs:15:16: 15:27
      let mut _6: &[u8];                   // in scope 0 at $DIR/lower_slice_len.rs:15:16: 15:21
      let mut _7: &[u8; N];                // in scope 0 at $DIR/lower_slice_len.rs:15:16: 15:21
      let _8: usize;                       // in scope 0 at $DIR/lower_slice_len.rs:16:15: 16:20
      let mut _9: usize;                   // in scope 0 at $DIR/lower_slice_len.rs:16:9: 16:21
      let mut _10: bool;                   // in scope 0 at $DIR/lower_slice_len.rs:16:9: 16:21
  
      bb0: {
          StorageLive(_3);                 // scope 0 at $DIR/lower_slice_len.rs:15:8: 15:27
          StorageLive(_4);                 // scope 0 at $DIR/lower_slice_len.rs:15:8: 15:13
          _4 = _1;                         // scope 0 at $DIR/lower_slice_len.rs:15:8: 15:13
          StorageLive(_5);                 // scope 0 at $DIR/lower_slice_len.rs:15:16: 15:27
          StorageLive(_6);                 // scope 0 at $DIR/lower_slice_len.rs:15:16: 15:21
          StorageLive(_7);                 // scope 0 at $DIR/lower_slice_len.rs:15:16: 15:21
          _7 = _2;                         // scope 0 at $DIR/lower_slice_len.rs:15:16: 15:21
          _6 = move _7 as &[u8] (Pointer(Unsize)); // scope 0 at $DIR/lower_slice_len.rs:15:16: 15:21
          StorageDead(_7);                 // scope 0 at $DIR/lower_slice_len.rs:15:20: 15:21
          _5 = Len((*_6));                 // scope 0 at $DIR/lower_slice_len.rs:15:16: 15:27
          StorageDead(_6);                 // scope 0 at $DIR/lower_slice_len.rs:15:26: 15:27
          _3 = Lt(move _4, move _5);       // scope 0 at $DIR/lower_slice_len.rs:15:8: 15:27
          StorageDead(_5);                 // scope 0 at $DIR/lower_slice_len.rs:15:26: 15:27
          StorageDead(_4);                 // scope 0 at $DIR/lower_slice_len.rs:15:26: 15:27
          switchInt(move _3) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/lower_slice_len.rs:15:5: 19:6
      }
  
      bb1: {
          StorageLive(_8);                 // scope 0 at $DIR/lower_slice_len.rs:16:15: 16:20
          _8 = _1;                         // scope 0 at $DIR/lower_slice_len.rs:16:15: 16:20
          _9 = const N;                    // scope 0 at $DIR/lower_slice_len.rs:16:9: 16:21
          _10 = Lt(_8, _9);                // scope 0 at $DIR/lower_slice_len.rs:16:9: 16:21
          assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> bb3; // scope 0 at $DIR/lower_slice_len.rs:16:9: 16:21
      }
  
      bb2: {
          _0 = const 42_u8;                // scope 0 at $DIR/lower_slice_len.rs:18:9: 18:11
          goto -> bb4;                     // scope 0 at $DIR/lower_slice_len.rs:15:5: 19:6
      }
  
      bb3: {
          _0 = (*_2)[_8];                  // scope 0 at $DIR/lower_slice_len.rs:16:9: 16:21
          StorageDead(_8);                 // scope 0 at $DIR/lower_slice_len.rs:17:5: 17:6
          goto -> bb4;                     // scope 0 at $DIR/lower_slice_len.rs:15:5: 19:6
      }
  
      bb4: {
          StorageDead(_3);                 // scope 0 at $DIR/lower_slice_len.rs:19:5: 19:6
          return;                          // scope 0 at $DIR/lower_slice_len.rs:20:2: 20:2
      }
  }

If I make a library modification then _5 = Len((*_6)); becomes _5 = N;

At the moment it's not even Len strictly speaking, but #86383 makes it so, before it was even core::slice::<impl [u8]>::len call instead of Len

@shamatar
Copy link
Contributor Author

r=@scottmcm It passes all the tests on local, can you please check my guess on making it insta-stable and may be start a CI and potentially crater run?

#[cfg(not(bootstrap))]
#[doc(alias = "length")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "array_len", since = "1.53.0")]
#[inline]
pub const fn len(&self) -> usize {

@shamatar
Copy link
Contributor Author

"Waiting for review" is not assigned even after r=@, is there any other way to touch it?

@bjorn3
Copy link
Member

bjorn3 commented Jun 20, 2021

r=@... is not an existing command. Note that comments of the kind r=me are just informal messages that it is fine for someone else with review permission to do @\bors r=<name of that person> once the review comments are deemed to be resolved by the one using bors.

Everyone can use the following though:

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot 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 Jun 20, 2021
@scottmcm scottmcm added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 21, 2021
@scottmcm
Copy link
Member

Since this is now insta-stable, I've marked it as needs-fcp and will send it over to

r? @m-ou-se

as they're active in the zulip conversation about it.

@rust-highfive rust-highfive assigned m-ou-se and unassigned scottmcm Jun 21, 2021
@shamatar
Copy link
Contributor Author

Workaround of this would be #86525

@bjorn3
Copy link
Member

bjorn3 commented Jun 23, 2021

@bors try @rust-timer queue

per the request in #86525 (comment)

@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 Jun 23, 2021
@bors
Copy link
Collaborator

bors commented Jun 23, 2021

⌛ Trying commit 189342d with merge 2e412375b3fd677ce07a0ddbd9809b06316d15d1...

@bors
Copy link
Collaborator

bors commented Jun 23, 2021

☀️ Try build successful - checks-actions
Build commit: 2e412375b3fd677ce07a0ddbd9809b06316d15d1 (2e412375b3fd677ce07a0ddbd9809b06316d15d1)

@rust-timer
Copy link
Collaborator

Queued 2e412375b3fd677ce07a0ddbd9809b06316d15d1 with parent 8cb207a, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit (2e412375b3fd677ce07a0ddbd9809b06316d15d1): 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. Please note that if the perf results are neutral, you should likely undo the rollup=never given below by specifying rollup- to bors.

Importantly, though, if the results of this run are non-neutral do not roll this PR up -- it will mask other regressions or improvements in the roll up.

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

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 23, 2021
@bjorn3
Copy link
Member

bjorn3 commented Jun 23, 2021

On average this seems to be a slight perf improvement up to 0.6%, but there are also several regressions up to 0.8%.

@shamatar
Copy link
Contributor Author

That performance run was to compare against MIR pass that achieves the same, but I'd expect it to be neutral at the end of the day, and it looks so

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 9, 2021
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 31, 2021
Comment on lines +402 to +403
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_stable(feature = "array_len", since = "1.53.0")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the #[stable] feature and version should be the same as the one for #[rustc_const_stable]?

@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2021
@camelid
Copy link
Member

camelid commented Oct 8, 2021

Workaround of this would be #86525

That PR has merged now, so I think this one can be closed, right?

@shamatar shamatar closed this Oct 8, 2021
@shamatar shamatar deleted the array_len branch February 29, 2024 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.