-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Mark TypeId::of as inline to avoid monomorphizing unnecessary code #74362
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
It would be cool if this calls get inlined in |
I found that this function always promoted to const on Another problem is that ParialEq::eq isn't inlined even if I use #![feature(const_type_id)]
#[inline(always)]
pub const fn is_bool<T: 'static>()->bool{
use std::any::TypeId;
const BOOL_ID: TypeId = TypeId::of::<bool>();
match TypeId::of::<T>(){
BOOL_ID => true,
_ => false,
}
}
pub fn good()->bool{
is_bool::<bool>()
}
pub fn bad()->bool{
is_bool::<u8>()
} |
I think, we should close this issue in favor of #77125 Or in favor of #31844 which allows write such code: #![feature(specialization)]
use std::marker::PhantomData;
struct IsSameType<T0: 'static, T1: 'static>(PhantomData<T0>, PhantomData<T1>);
trait IsSameTypeTrait{
const IS_SAME: bool;
}
impl<T0: 'static, T1: 'static> IsSameTypeTrait for IsSameType<T0, T1>{
default const IS_SAME: bool = false;
}
impl<T> IsSameTypeTrait for IsSameType<T, T>{
const IS_SAME: bool = true;
} |
Adding With New Pass Manager (which I hope would stabilized soon), users may set |
…62, r=dtolnay Add `#[inline]` modifier to `TypeId::of` It was already inlined but it happened only in 4th InlinerPass on my testcase. With `#[inline]` modifier it happens on 2nd pass. Closes rust-lang#74362
Uh oh!
There was an error while loading. Please reload this page.
TypeId
can be used to specialize code by comparing types explicitly (TypeId::of::<u8>() == TypeId::of::<T>()
, examples in the wild) or implicitly (<dyn Any>::downcast_mut
).In release mode this works well; in debug mode though the unused impl is unnecessarily monomorphized and codegened, slowing compile time as well as run time.
It isn't a major issue as it only affects debug mode, but I wondered if it was worth marking
TypeId::of
,<TypeId as PartialEq>::eq
,<dyn Any>::is
and the various downcast methods asinline(always)
to potentially reduce compile times and provide a nice debug mode runtime boost?See e.g.
abort
is present in IR/assembly: PlaygroundThe text was updated successfully, but these errors were encountered: