-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Optimize LazyLock
size
#107329
Optimize LazyLock
size
#107329
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
Since this optimizes the size, it would be nice to add a |
I'm a bit hesitant to do so because the size is very platform and compiler specific. Also, I'm not really convinced that a test is necessary here. If required, however, would something like this suffice? /// Test that the total size is less than the size required to store both the closure
/// and the value at the same time.
#[test]
fn test_size() {
const SIZE: usize = 1024;
type InitFn = impl FnOnce() -> [u8; SIZE];
fn init_fn(data: [u8; SIZE]) -> InitFn {
move || data
}
assert!(size_of::<LazyLock<[u8; SIZE], InitFn>>() < size_of::<Once>() + SIZE + size_of::<InitFn>());
} |
The size asserts are usually checked only for rust/compiler/rustc_ast/src/tokenstream.rs Line 644 in c62665e
|
The problem is that even on systems with the same pointer size, |
match self.get() { | ||
Some(v) => f.debug_tuple("LazyLock").field(v).finish(), | ||
None => f.write_str("LazyLock(Uninit)"), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: With a #[derive(Debug)] struct Uninit;
, a fully initialized LazyLock<Uninit>
would also produce "LazyLock(Uninit)"
. Maybe it's better to show a nested Option
(LazyLock(Some(..))
), or to use LazyLock(<uninitialized>)
for the uninitialized case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see we also already format an empty OnceCell
as OnceCell(Uninit)
. So no need to change that in this PR.
@bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (3701bdc): comparison URL. Overall result: ❌✅ regressions and improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesThis benchmark run did not return any relevant results for this metric. |
…ulacrum Optimize `LazyCell` size `LazyCell` can only store either the initializing function or the data it produces, so it does not need to reserve the space for both. Similar to rust-lang#107329, but uses an `enum` instead of a `union`.
If this new |
I don't anticipate a need for it, considering that |
The initialization function was unnecessarily stored separately from the data to be initialized. Since both cannot exist at the same time, a
union
can be used, with theOnce
acting as discriminant. This unfortunately requires some extra methods onOnce
so thatDrop
can be implemented correctly and efficiently.@rustbot label +T-libs +A-atomic