diff --git a/starlark-rust/starlark/src/values/starlark_type_id.rs b/starlark-rust/starlark/src/values/starlark_type_id.rs index abc441057426..4ba5c7c170f6 100644 --- a/starlark-rust/starlark/src/values/starlark_type_id.rs +++ b/starlark-rust/starlark/src/values/starlark_type_id.rs @@ -47,3 +47,33 @@ impl StarlarkTypeId { StarlarkTypeId(ConstTypeId::of::()) } } + +/// We require alignment 8 for `StarlarkValue`. +/// `TypeId` is 16 bytes aligned on Rust 1.72 on Apple Silicon. +/// Use this struct to put `ConstTypeId` in a `StarlarkValue`. +// TODO(nga): remove alignment requirement from `Heap`. +#[repr(packed(8))] +#[derive(Allocative, Eq, Clone, Copy, Dupe, Debug)] +#[allocative(skip)] // There are no heap allocations in this struct. +pub(crate) struct StarlarkTypeIdAligned { + starlark_type_id: StarlarkTypeId, +} + +impl PartialEq for StarlarkTypeIdAligned { + #[inline] + fn eq(&self, other: &Self) -> bool { + self.get() == other.get() + } +} + +impl StarlarkTypeIdAligned { + #[inline] + pub(crate) const fn new(starlark_type_id: StarlarkTypeId) -> StarlarkTypeIdAligned { + StarlarkTypeIdAligned { starlark_type_id } + } + + #[inline] + pub(crate) const fn get(&self) -> StarlarkTypeId { + self.starlark_type_id + } +} diff --git a/starlark-rust/starlark/src/values/typing/type_compiled/matchers.rs b/starlark-rust/starlark/src/values/typing/type_compiled/matchers.rs index 7288ef490216..7a8ea5712208 100644 --- a/starlark-rust/starlark/src/values/typing/type_compiled/matchers.rs +++ b/starlark-rust/starlark/src/values/typing/type_compiled/matchers.rs @@ -27,6 +27,7 @@ use crate::values::dict::DictRef; use crate::values::list::value::FrozenList; use crate::values::list::ListRef; use crate::values::starlark_type_id::StarlarkTypeId; +use crate::values::starlark_type_id::StarlarkTypeIdAligned; use crate::values::tuple::value::Tuple; use crate::values::types::int_or_big::StarlarkIntRef; use crate::values::typing::type_compiled::matcher::TypeMatcher; @@ -245,20 +246,20 @@ impl TypeMatcher for IsNone { #[derive(Allocative, Debug, Clone)] pub(crate) struct StarlarkTypeIdMatcher { - starlark_type_id: StarlarkTypeId, + starlark_type_id: StarlarkTypeIdAligned, } impl StarlarkTypeIdMatcher { pub(crate) fn new(ty: TyStarlarkValue) -> StarlarkTypeIdMatcher { StarlarkTypeIdMatcher { - starlark_type_id: ty.starlark_type_id(), + starlark_type_id: StarlarkTypeIdAligned::new(ty.starlark_type_id()), } } } impl TypeMatcher for StarlarkTypeIdMatcher { fn matches(&self, value: Value) -> bool { - value.starlark_type_id() == self.starlark_type_id + value.starlark_type_id() == self.starlark_type_id.get() } }