Skip to content

Commit 7249af9

Browse files
committed
Move typeid into vtable
1 parent 9e7d291 commit 7249af9

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

vm/src/builtins/str.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,12 +1959,10 @@ impl PyPayload for PyUtf8Str {
19591959
ctx.types.str_type
19601960
}
19611961

1962-
fn payload_type_id() -> std::any::TypeId {
1963-
std::any::TypeId::of::<PyStr>()
1964-
}
1962+
const PAYLOAD_TYPE_ID: std::any::TypeId = std::any::TypeId::of::<PyStr>();
19651963

19661964
fn downcastable_from(obj: &PyObject) -> bool {
1967-
obj.typeid() == Self::payload_type_id() && {
1965+
obj.typeid() == Self::PAYLOAD_TYPE_ID && {
19681966
// SAFETY: we know the object is a PyStr in this context
19691967
let wtf8 = unsafe { obj.downcast_unchecked_ref::<PyStr>() };
19701968
wtf8.is_utf8()

vm/src/object/core.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ pub(super) unsafe fn try_trace_obj<T: PyObjectPayload>(
103103
#[repr(C)]
104104
pub(super) struct PyInner<T> {
105105
pub(super) ref_count: RefCount,
106-
// TODO: move typeid into vtable once TypeId::of is const
107-
pub(super) typeid: TypeId,
108106
pub(super) vtable: &'static PyObjVTable,
109107

110108
pub(super) typ: PyAtomicRef<PyType>, // __class__ member
@@ -446,7 +444,6 @@ impl<T: PyObjectPayload> PyInner<T> {
446444
let member_count = typ.slots.member_count;
447445
Box::new(Self {
448446
ref_count: RefCount::new(),
449-
typeid: T::payload_type_id(),
450447
vtable: PyObjVTable::of::<T>(),
451448
typ: PyAtomicRef::from(typ),
452449
dict: dict.map(InstanceDict::new),
@@ -639,7 +636,7 @@ impl PyObject {
639636
#[deprecated(note = "use downcastable instead")]
640637
#[inline(always)]
641638
pub fn payload_is<T: PyObjectPayload>(&self) -> bool {
642-
self.0.typeid == T::payload_type_id()
639+
self.0.vtable.typeid == T::PAYLOAD_TYPE_ID
643640
}
644641

645642
/// Force to return payload as T.
@@ -725,7 +722,7 @@ impl PyObject {
725722

726723
#[inline]
727724
pub(crate) fn typeid(&self) -> TypeId {
728-
self.0.typeid
725+
self.0.vtable.typeid
729726
}
730727

731728
/// Check if this object can be downcast to T.
@@ -1229,7 +1226,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
12291226
let type_type_ptr = Box::into_raw(Box::new(partially_init!(
12301227
PyInner::<PyType> {
12311228
ref_count: RefCount::new(),
1232-
typeid: TypeId::of::<PyType>(),
12331229
vtable: PyObjVTable::of::<PyType>(),
12341230
dict: None,
12351231
weak_list: WeakRefList::new(),
@@ -1241,7 +1237,6 @@ pub(crate) fn init_type_hierarchy() -> (PyTypeRef, PyTypeRef, PyTypeRef) {
12411237
let object_type_ptr = Box::into_raw(Box::new(partially_init!(
12421238
PyInner::<PyType> {
12431239
ref_count: RefCount::new(),
1244-
typeid: TypeId::of::<PyType>(),
12451240
vtable: PyObjVTable::of::<PyType>(),
12461241
dict: None,
12471242
weak_list: WeakRefList::new(),

vm/src/object/payload.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ cfg_if::cfg_if! {
1919
pub trait PyPayload:
2020
std::fmt::Debug + MaybeTraverse + PyThreadingConstraint + Sized + 'static
2121
{
22-
#[inline]
23-
fn payload_type_id() -> std::any::TypeId {
24-
std::any::TypeId::of::<Self>()
25-
}
22+
const PAYLOAD_TYPE_ID: std::any::TypeId = std::any::TypeId::of::<Self>();
2623

2724
/// # Safety: this function should only be called if `payload_type_id` matches the type of `obj`.
2825
#[inline]
2926
fn downcastable_from(obj: &PyObject) -> bool {
30-
obj.typeid() == Self::payload_type_id()
27+
obj.typeid() == Self::PAYLOAD_TYPE_ID
3128
}
3229

3330
fn try_downcast_from(obj: &PyObject, vm: &VirtualMachine) -> PyResult<()> {

vm/src/object/traverse_object.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::any::TypeId;
12
use std::fmt;
23

34
use crate::{
@@ -11,6 +12,7 @@ use crate::{
1112
use super::{Traverse, TraverseFn};
1213

1314
pub(in crate::object) struct PyObjVTable {
15+
pub(in crate::object) typeid: TypeId,
1416
pub(in crate::object) drop_dealloc: unsafe fn(*mut PyObject),
1517
pub(in crate::object) debug: unsafe fn(&PyObject, &mut fmt::Formatter<'_>) -> fmt::Result,
1618
pub(in crate::object) trace: Option<unsafe fn(&PyObject, &mut TraverseFn<'_>)>,
@@ -19,6 +21,7 @@ pub(in crate::object) struct PyObjVTable {
1921
impl PyObjVTable {
2022
pub const fn of<T: PyObjectPayload>() -> &'static Self {
2123
&Self {
24+
typeid: T::PAYLOAD_TYPE_ID,
2225
drop_dealloc: drop_dealloc_obj::<T>,
2326
debug: debug_obj::<T>,
2427
trace: const {

0 commit comments

Comments
 (0)