Use constant for detecting thin pointer formatting#157933
Conversation
|
r? @Darksonn rustbot has assigned @Darksonn. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This allows codegen to prune the unnecessary side of the `if` for each pointer type during monomorphization. LLVM optimizations can clean it up too, but it's better to not emit unnecessary code in the first place.
06557a2 to
38f4f4a
Compare
| true | ||
| } | ||
| // `type_id` erases lifetimes, but that's OK here because "is it ()" never depends on lifetimes | ||
| const IS_UNIT: bool = type_id::<Self>() == type_id::<()>(); |
There was a problem hiding this comment.
Note that this replaces use of specialization with use of non-'static TypeId. I'm not sure if that's better or worse as far as using unstable features in std is concerned. The change to the intrinsic that makes this work is relatively recent, and part of the experimental reflection work. On the other hand, non-'static TypeId is implementable on stable.
|
Example showing the impact: https://gist.github.com/hanna-kruppe/e022d3c770eb692c567c401194e1fd35 |
| let ptr: *const T = *self; | ||
| let ptr_addr = ptr.expose_provenance(); | ||
| if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::is_unit() { | ||
| if <<T as core::ptr::Pointee>::Metadata as core::unit::IsUnit>::IS_UNIT { |
There was a problem hiding this comment.
I also considered just detecting ZST metadata, since it shouldn't make a difference and SizedTypeProperties::IS_ZST already exists, but detecting () specifically was requested in the PR that added this code: #135080 (comment)
…n, r=Darksonn Use constant for detecting thin pointer formatting This allows codegen to prune the unnecessary side of the `if` for each pointer type during monomorphization. In release builds, LLVM can clean it up, but it's better to not emit unnecessary code in the first place.
This allows codegen to prune the unnecessary side of the
iffor each pointer type during monomorphization. In release builds, LLVM can clean it up, but it's better to not emit unnecessary code in the first place.