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 #[inline] markers to once_cell methods #105651

Merged
merged 1 commit into from
Dec 30, 2022

Conversation

tgross35
Copy link
Contributor

Added inline markers to all simple methods under the once_cell feature. Relates to #74465 and #105587

This should not block #105587

@rustbot
Copy link
Collaborator

rustbot commented Dec 13, 2022

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

Please see the contribution instructions for more information.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 13, 2022
@rustbot
Copy link
Collaborator

rustbot commented Dec 13, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@tgross35
Copy link
Contributor Author

I'm not sure what provides the guidelines for what to mark #[inline] and what not in std, so I will update if needed

@tgross35 tgross35 changed the title Add #[inline] markers to OnceCell/LazyCell/OnceLock/LazyLock Add #[inline] markers to once_cell methods Dec 13, 2022
@the8472
Copy link
Member

the8472 commented Dec 13, 2022

The PR description doesn't provide a motivation for this change. Are there benchmarks showing improvement? Generic methods already are available for inlining across crate boundaries, so adding #[inline] would only affect how they're spread across CGUs.

@tgross35
Copy link
Contributor Author

The motivation largely comes from comparison to Cell and RefCell, which have similar inline patterns that I assume are well proven.

Are you referring to benchmarks of rustc compile times or of runtime lib performance? If they are required, I could look into how to do them (it would take me a bit)

@the8472
Copy link
Member

the8472 commented Dec 13, 2022

See https://std-dev-guide.rust-lang.org/code-considerations/performance/inline.html

Are you referring to benchmarks of rustc compile times or of runtime lib performance? If they are required, I could look into how to do them (it would take me a bit)

I'm asking if there are motivating benchmarks. Code that already exists and is slow due to the absence of these annotations.

@saethlin
Copy link
Member

adding #[inline] would only affect how they're spread across CGUs.

I almost hate to bring this up, but adding the attribute makes these functions eligible for MIR inlining:

return Err("at mir-opt-level=2, only #[inline] is inlined");

@m-ou-se
Copy link
Member

m-ou-se commented Dec 30, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Dec 30, 2022

📌 Commit b9558a1 has been approved by m-ou-se

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 Dec 30, 2022
@bors
Copy link
Contributor

bors commented Dec 30, 2022

⌛ Testing commit b9558a1 with merge ce85c98...

@bors
Copy link
Contributor

bors commented Dec 30, 2022

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing ce85c98 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 30, 2022
@bors bors merged commit ce85c98 into rust-lang:master Dec 30, 2022
@rustbot rustbot added this to the 1.68.0 milestone Dec 30, 2022
@tgross35 tgross35 deleted the once-cell-inline branch December 30, 2022 22:35
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ce85c98): comparison URL.

Overall result: ❌ regressions - 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.6% [0.6%, 0.6%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.6% [0.6%, 0.6%] 1

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)
2.5% [1.4%, 3.3%] 5
Regressions ❌
(secondary)
2.2% [0.8%, 3.8%] 37
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.0% [-5.0%, -5.0%] 1
All ❌✅ (primary) 2.5% [1.4%, 3.3%] 5

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

Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Add #[inline] markers to once_cell methods

Added inline markers to all simple methods under the `once_cell` feature. Relates to rust-lang#74465 and  rust-lang#105587

This should not block rust-lang#105587
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 30, 2023
Partial stabilization of `once_cell`

This PR aims to stabilize a portion of the `once_cell` feature:

- `core::cell::OnceCell`
- `std::cell::OnceCell` (re-export of the above)
- `std::sync::OnceLock`

This will leave `LazyCell` and `LazyLock` unstabilized, which have been moved to the `lazy_cell` feature flag.

Tracking issue: rust-lang#74465 (does not fully close, but it may make sense to move to a new issue)

Future steps for separate PRs:
- ~~Add `#[inline]` to many methods~~ rust-lang#105651
- Update cranelift usage of the `once_cell` crate
- Update rust-analyzer usage of the `once_cell` crate
- Update error messages discussing once_cell

## To be stabilized API summary

```rust
// core::cell (in core/cell/once.rs)

pub struct OnceCell<T> { .. }

impl<T> OnceCell<T> {
    pub const fn new() -> OnceCell<T>;
    pub fn get(&self) -> Option<&T>;
    pub fn get_mut(&mut self) -> Option<&mut T>;
    pub fn set(&self, value: T) -> Result<(), T>;
    pub fn get_or_init<F>(&self, f: F) -> &T where F: FnOnce() -> T;
    pub fn into_inner(self) -> Option<T>;
    pub fn take(&mut self) -> Option<T>;
}

impl<T: Clone> Clone for OnceCell<T>;
impl<T: Debug> Debug for OnceCell<T>
impl<T> Default for OnceCell<T>;
impl<T> From<T> for OnceCell<T>;
impl<T: PartialEq> PartialEq for OnceCell<T>;
impl<T: Eq> Eq for OnceCell<T>;
```

```rust
// std::sync (in std/sync/once_lock.rs)

impl<T> OnceLock<T> {
    pub const fn new() -> OnceLock<T>;
    pub fn get(&self) -> Option<&T>;
    pub fn get_mut(&mut self) -> Option<&mut T>;
    pub fn set(&self, value: T) -> Result<(), T>;
    pub fn get_or_init<F>(&self, f: F) -> &T where F: FnOnce() -> T;
    pub fn into_inner(self) -> Option<T>;
    pub fn take(&mut self) -> Option<T>;
}

impl<T: Clone> Clone for OnceLock<T>;
impl<T: Debug> Debug for OnceLock<T>;
impl<T> Default for OnceLock<T>;
impl<#[may_dangle] T> Drop for OnceLock<T>;
impl<T> From<T> for OnceLock<T>;
impl<T: PartialEq> PartialEq for OnceLock<T>
impl<T: Eq> Eq for OnceLock<T>;
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceLock<T>;
unsafe impl<T: Send> Send for OnceLock<T>;
unsafe impl<T: Sync + Send> Sync for OnceLock<T>;
impl<T: UnwindSafe> UnwindSafe for OnceLock<T>;
```

No longer planned as part of this PR, and moved to the `rust_cell_try` feature gate:

```rust
impl<T> OnceCell<T> {
    pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>;
}

impl<T> OnceLock<T> {
    pub fn get_or_try_init<F, E>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>;
}
```

I am new to this process so would appreciate mentorship wherever needed.
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. 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