Skip to content

Commit 73364fa

Browse files
committed
try &'static str for slots.name
1 parent 44f6446 commit 73364fa

File tree

5 files changed

+25
-37
lines changed

5 files changed

+25
-37
lines changed

vm/src/builtins/genericalias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl PyGenericAlias {
214214
}
215215
}
216216

217-
fn is_typevar(obj: &PyObjectRef, vm: &VirtualMachine) -> bool {
217+
pub(crate) fn is_typevar(obj: &PyObjectRef, vm: &VirtualMachine) -> bool {
218218
let class = obj.class();
219219
"TypeVar" == &*class.slot_name()
220220
&& class

vm/src/builtins/type.rs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,18 @@ impl PyType {
142142
metaclass: PyRef<Self>,
143143
ctx: &Context,
144144
) -> Result<PyRef<Self>, String> {
145+
// TODO: ensure clean slot name
146+
// assert_eq!(slots.name.borrow(), "");
147+
145148
let name = ctx.new_str(name);
146-
let (name_str, heaptype_ext) = unsafe {
147-
// # Safety
148-
// `name_str` live long enough because `heaptype_ext` is alive.
149-
let name_str = &*(name.as_str() as *const _);
150-
let heaptype_ext = HeapTypeExt {
151-
name: PyRwLock::new(name),
152-
slots: None,
153-
number_methods: PyNumberMethods::default(),
154-
sequence_methods: PySequenceMethods::default(),
155-
mapping_methods: PyMappingMethods::default(),
156-
};
157-
(name_str, heaptype_ext)
149+
let heaptype_ext = HeapTypeExt {
150+
name: PyRwLock::new(name),
151+
slots: None,
152+
number_methods: PyNumberMethods::default(),
153+
sequence_methods: PySequenceMethods::default(),
154+
mapping_methods: PyMappingMethods::default(),
158155
};
159156
let base = bases[0].clone();
160-
let _prev_name = slots.name.swap(name_str);
161-
// TODO: ensure clean slot name
162-
// assert!(
163-
// prev_name.is_empty() || prev_name == base.slots.name.load(),
164-
// "{prev_name:?} {}",
165-
// base.slots.name.load(),
166-
// );
167157

168158
Self::new_heap_inner(base, bases, attrs, slots, heaptype_ext, metaclass, ctx)
169159
}
@@ -380,7 +370,7 @@ impl PyType {
380370
heap_f: impl FnOnce(&'a HeapTypeExt) -> R,
381371
) -> R {
382372
if !self.slots.flags.has_feature(PyTypeFlags::HEAPTYPE) {
383-
static_f(self.slots.name.load())
373+
static_f(self.slots.name)
384374
} else {
385375
heap_f(self.heaptype_ext.as_ref().unwrap())
386376
}
@@ -452,7 +442,7 @@ impl PyType {
452442
.unwrap_or_else(|| {
453443
panic!(
454444
"static type name must be already interned but {} is not",
455-
self.slots.name.load()
445+
self.slot_name()
456446
)
457447
})
458448
.to_owned()
@@ -886,7 +876,7 @@ impl PyType {
886876
return Err(vm.new_type_error(format!(
887877
"cannot set '{}' attribute of immutable type '{}'",
888878
name,
889-
self.slots.name.load()
879+
self.slot_name()
890880
)));
891881
}
892882
Ok(())
@@ -898,21 +888,16 @@ impl PyType {
898888
let name = value.downcast::<PyStr>().map_err(|value| {
899889
vm.new_type_error(format!(
900890
"can only assign string to {}.__name__, not '{}'",
901-
self.slots.name.load(),
902-
value.class().slots.name.load(),
891+
self.slot_name(),
892+
value.class().slot_name(),
903893
))
904894
})?;
905895
if name.as_str().as_bytes().contains(&0) {
906896
return Err(vm.new_value_error("type name must not contain null characters".to_owned()));
907897
}
908898

909-
unsafe {
910-
// # Safety
911-
// changing slots.name while holding __name__ lock is safe
912-
let mut name_lock = self.heaptype_ext.as_ref().unwrap().name.write();
913-
self.slots.name.store(&*(name.as_str() as *const _));
914-
*name_lock = name;
915-
}
899+
*self.heaptype_ext.as_ref().unwrap().name.write() = name;
900+
916901
Ok(())
917902
}
918903

vm/src/class.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
types::{hash_not_implemented, PyTypeFlags, PyTypeSlots},
88
vm::Context,
99
};
10-
use crossbeam_utils::atomic::AtomicCell;
1110
use rustpython_common::static_cell;
1211

1312
pub trait StaticType {
@@ -132,7 +131,7 @@ pub trait PyClassImpl: PyClassDef {
132131
fn make_slots() -> PyTypeSlots {
133132
let mut slots = PyTypeSlots {
134133
flags: Self::TP_FLAGS,
135-
name: AtomicCell::new(Self::TP_NAME),
134+
name: Self::TP_NAME,
136135
basicsize: Self::BASICSIZE,
137136
doc: Self::DOC,
138137
..Default::default()

vm/src/types/slot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct PyTypeSlots {
3333
/// # Safety
3434
/// For static types, always safe.
3535
/// For heap types, `__name__` must alive
36-
pub(crate) name: AtomicCell<&'static str>, // tp_name with <module>.<class> for print, not class name
36+
pub(crate) name: &'static str, // tp_name with <module>.<class> for print, not class name
3737

3838
pub basicsize: usize,
3939
// tp_itemsize
@@ -96,7 +96,7 @@ pub struct PyTypeSlots {
9696
impl PyTypeSlots {
9797
pub fn new(name: &'static str, flags: PyTypeFlags) -> Self {
9898
Self {
99-
name: AtomicCell::new(name),
99+
name,
100100
flags,
101101
..Default::default()
102102
}

vm/src/vm/context.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,15 @@ impl Context {
466466
let mut attrs = PyAttributes::default();
467467
attrs.insert(identifier!(self, __module__), self.new_str(module).into());
468468

469+
let interned_name = self.intern_str(name);
469470
PyType::new_heap(
470471
name,
471472
bases,
472473
attrs,
473-
PyBaseException::make_slots(),
474+
PyTypeSlots {
475+
name: interned_name.as_str(),
476+
..PyBaseException::make_slots()
477+
},
474478
self.types.type_type.to_owned(),
475479
self,
476480
)

0 commit comments

Comments
 (0)