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

Implement IzZero for zeroable default types #60978

Open
Pzixel opened this issue May 20, 2019 · 6 comments
Open

Implement IzZero for zeroable default types #60978

Pzixel opened this issue May 20, 2019 · 6 comments
Labels
A-collections Area: `std::collection` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Pzixel
Copy link

Pzixel commented May 20, 2019

As discussed in #54628 there are some critical optimizations based on fact that type default value can be represented with mem::zeroed(), such as this vec allocation code.

There is a trait describing this behavior. I propose implementing it for some standard types, such as std::num::Wrapping or Option, or even making it public.

@jonas-schievink
Copy link
Contributor

That trait is only used by internal Windows things. It probably does not have the semantics you're looking for.

@jonas-schievink
Copy link
Contributor

Ah, did you mean to link this trait instead?

rust/src/liballoc/vec.rs

Lines 1594 to 1597 in 372be4f

unsafe trait IsZero {
/// Whether this value is zero
fn is_zero(&self) -> bool;
}

@jonas-schievink jonas-schievink added A-collections Area: `std::collection` T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels May 20, 2019
@Pzixel
Copy link
Author

Pzixel commented May 20, 2019

@jonas-schievink yes, I linked the wrong trait, fixed. Thanks.

@hellow554
Copy link
Contributor

Is Option::None really always 0? I doubt that, nor have I examples for that :)

@sinkuu
Copy link
Contributor

sinkuu commented May 21, 2019

cc #58628 (comment)

@the8472
Copy link
Member

the8472 commented May 26, 2019

Is Option::None really always 0? I doubt that, nor have I examples for that :)

let foo : Option<bool> = Option::None;
println!("size:{} val:{:08b}",
          std::mem::size_of::<Option<bool>>(),
          unsafe {std::mem::transmute::<Option<bool>, u8>(foo)});
// size:1 val:00000010

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 21, 2019
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 4, 2022
Implement internal `IsZero` for Wrapping and Saturating for `Vec` optimizations

This implements the `IsZero` trait for the `Wrapping` and `Saturating` types so that users of these types can get the improved performance from the specialization of creating a `Vec` from a single element repeated when it has a zero bit pattern (example `vec![0_i32; 500]`, or after this PR `vec![Wrapping(0_i32); 500]`)

CC rust-lang#60978
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants