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

Migrate from lazy_static to once_cell #2313

Merged
merged 6 commits into from
Jun 8, 2022
Merged

Migrate from lazy_static to once_cell #2313

merged 6 commits into from
Jun 8, 2022

Conversation

james7132
Copy link
Contributor

  • Tested on all platforms changed
  • Added an entry to CHANGELOG.md if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality
  • Updated feature matrix, if new features were added or implemented

Motivation

lazy_static!, while a declarative macro, is a macro nonetheless. It can add quite a bit of additional compilation time cost. once_cell::sync::Lazy does the same thing with generics, and can be used more flexibly (i.e. non-static lazily initialized values), and has been proposed to be added to std.

I'm trying to reduce the compile time and dependency tree complexity of a dependent project: bevy, which is using winit. lazy_static and once_cell are both in our dependency tree and both end up doing the same thing.

Solution

Migrate to once_cell.


I don't think any of the changes here are user visible beyond the dependency in the tree, and should work as-is on all of them, but I could be wrong.

@madsmtm madsmtm added the S - maintenance Repaying technical debt label Jun 6, 2022
Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

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

Sure, if it helps you. This is likely going to be closer to the final std API anyhow, and then the switch will be easier when we get there.

Copy link
Member

@maroider maroider left a comment

Choose a reason for hiding this comment

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

Mostly nitpicks from me. I don't quite get why you've grouped "external crate" imports with/above std imports. I was under the impression that std -> extern crate -> local crate is the "normal" way of grouping imports (or at least quite popular, as it's the only option besides "preserve" in rustfmt's configuration).

src/platform_impl/macos/app_state.rs Outdated Show resolved Hide resolved
src/platform_impl/macos/window.rs Outdated Show resolved Hide resolved
Copy link
Member

@kchibisov kchibisov left a comment

Choose a reason for hiding this comment

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

I don't quite get why you've grouped "external crate" imports with/above std imports. I was under the impression that std -> extern crate -> local crate

I agree with @maroider . external crates should be separate from std. It just winit doesn't have formatting that way across the code base, but it doesn't mean that we should break it even more.

@james7132
Copy link
Contributor Author

Should have fixed the spurious moves as a result of cargo fmt --all, including some of the aforementioned broken ones.

Is there a way to add this via a rustfmt.toml? Seems like a trivial thing for any contributor to just run the command and undo all of these.

@madsmtm
Copy link
Member

madsmtm commented Jun 8, 2022

Is there a way to add this via a rustfmt.toml? Seems like a trivial thing for any contributor to just run the command and undo all of these.

There will be, it's just not stable yet.

Copy link
Member

@maroider maroider left a comment

Choose a reason for hiding this comment

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

Everything seems OK now. Like, @madsmtm, I'm of the opinion that we should move to the std API when that stabilizes, and this will make that switch easier.

@kchibisov kchibisov merged commit 2c01e9e into rust-windowing:master Jun 8, 2022
@MarijnS95
Copy link
Member

There will be, it's just not stable yet.

Would it make sense to require this in the CI at least? Strangely enough the option works on a stable rustfmt when passed on the commandline, so it doesn't seem too harsh to request contributors to manually invoke something like cargo fmt -- --config imports_granularity=crate --config group_imports=stdexternalcrate.

Otherwise future PRs might accidentally break or not adhere to this "unwritten" convention, and puts the burden on reviewers to make sure things stay sane. Lots of files don't fully adhere to it yet either.

@kchibisov
Copy link
Member

Or we require running rustfmt on nightly and run it on nightly on CI. That's how we deal with it in alacritty for example. On CI we install latest available rustfmt from nightly and run formatter.

MarijnS95 added a commit to rust-mobile/ndk that referenced this pull request Jun 10, 2022
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier for us to switch to a standardized version if/when that
happens.  The author of that winit PR is making this change to many more
crates, slowly turning the scales in favour of `once_cell` in our
dependency tree too.

Furthermore `lazy_static` hasn't published any updates for 3 years, and
the new syntax is closer for dropping this wrapping completely when the
necessary constructors become `const` (i.e. switching to `parking_lot`
will give us a [`const fn new()` on `RwLock`]) or this feature lands in stable
`std`.

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[`const fn new()` on `RwLock`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLock.html#method.new
MarijnS95 added a commit to rust-mobile/ndk that referenced this pull request Jun 10, 2022
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier for us to switch to a standardized version if/when that
happens.  The author of that winit PR is making this change to many more
crates, slowly turning the scales in favour of `once_cell` in our
dependency tree too.

Furthermore `lazy_static` hasn't published any updates for 3 years, and
the new syntax is closer for dropping this wrapping completely when the
necessary constructors become `const` (i.e. switching to `parking_lot`
will give us a [`const fn new()` on `RwLock`]) or this feature lands in stable
`std`.

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[`const fn new()` on `RwLock`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLock.html#method.new
@MarijnS95
Copy link
Member

@kchibisov I don't really mind which rustfmt to use, given that stable seems to work iff the config option is passed on the cmdline.

Would you do the honors and PR that rustfmt from alacritty here?

MarijnS95 added a commit to MarijnS95/which-rs that referenced this pull request Jun 11, 2022
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier to switch to a standardized version if/when that happens.  The
author of that winit PR is making this change to many more crates,
slowly turning the scales in favour of `once_cell` in most dependency
trees.  Furthermore `lazy_static` hasn't published any updates for 3
years.

See also [the `once_cell` F.A.Q.].

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[the `once_cell` F.A.Q.]: https://docs.rs/once_cell/latest/once_cell/#faq
MarijnS95 added a commit to MarijnS95/sentry-rust that referenced this pull request Jun 11, 2022
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier to switch to a standardized version if/when that happens.  The
author of that winit PR is making this change to many more crates,
slowly turning the scales in favour of `once_cell` in most dependency
trees.  Furthermore `lazy_static` hasn't published any updates for 3
years.

See also [the `once_cell` F.A.Q.].

In addition "constant" `Vec`tor allocations don't need to be wrapped in
a `lazy_static!` macro call at all but can be replaced with true `const`
slices (or `const` sized arrays, but those are slightly more tedious to
construct).

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[the `once_cell` F.A.Q.]: https://docs.rs/once_cell/latest/once_cell/#faq
MarijnS95 added a commit to MarijnS95/sentry-rust that referenced this pull request Jun 11, 2022
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier to switch to a standardized version if/when that happens.  The
author of that winit PR is making this change to many more crates,
slowly turning the scales in favour of `once_cell` in most dependency
trees.  Furthermore `lazy_static` hasn't published any updates for 3
years.

See also [the `once_cell` F.A.Q.].

In addition "constant" `Vec`tor allocations don't need to be wrapped in
a `lazy_static!` macro call at all but can be replaced with true `const`
slices (or `const` sized arrays, but those are slightly more tedious to
construct).

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[the `once_cell` F.A.Q.]: https://docs.rs/once_cell/latest/once_cell/#faq
MarijnS95 added a commit to MarijnS95/app_dirs2 that referenced this pull request Jun 12, 2022
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier to switch to a standardized version if/when that happens.  The
author of that winit PR is making this change to many more crates,
slowly turning the scales in favour of `once_cell` in most dependency
trees (despite this being a `dev-dependency` used in tests exclusively,
hence not relevant for downstream crates).  Furthermore `lazy_static`
hasn't published any updates for 3 years.

See also [the `once_cell` F.A.Q.].

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[the `once_cell` F.A.Q.]: https://docs.rs/once_cell/latest/once_cell/#faq
Swatinem pushed a commit to getsentry/sentry-rust that referenced this pull request Jun 13, 2022
… slices (#471)

Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier to switch to a standardized version if/when that happens.  The
author of that winit PR is making this change to many more crates,
slowly turning the scales in favour of `once_cell` in most dependency
trees.  Furthermore `lazy_static` hasn't published any updates for 3
years.

See also [the `once_cell` F.A.Q.].

In addition "constant" `Vec`tor allocations don't need to be wrapped in
a `lazy_static!` macro call at all but can be replaced with true `const`
slices (or `const` sized arrays, but those are slightly more tedious to
construct).

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[the `once_cell` F.A.Q.]: https://docs.rs/once_cell/latest/once_cell/#faq
rib pushed a commit to rust-mobile/ndk-glue that referenced this pull request Dec 6, 2022
Piggybacking on the [motivation in winit]: `lazy_static!` is a macro
whereas `once_cell` achieves the same using generics.  Its
implementation has also been [proposed for inclusion in `std`], making
it easier for us to switch to a standardized version if/when that
happens.  The author of that winit PR is making this change to many more
crates, slowly turning the scales in favour of `once_cell` in our
dependency tree too.

Furthermore `lazy_static` hasn't published any updates for 3 years, and
the new syntax is closer for dropping this wrapping completely when the
necessary constructors become `const` (i.e. switching to `parking_lot`
will give us a [`const fn new()` on `RwLock`]) or this feature lands in stable
`std`.

[motivation in winit]: rust-windowing/winit#2313
[proposed for inclusion in `std`]: rust-lang/rust#74465
[`const fn new()` on `RwLock`]: https://docs.rs/lock_api/latest/lock_api/struct.RwLock.html#method.new
@james7132 james7132 mentioned this pull request Feb 24, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S - maintenance Repaying technical debt
Development

Successfully merging this pull request may close these issues.

6 participants