8000 fix: elide null check in vtable&CR refactor · RustPython/RustPython@99e9257 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99e9257

Browse files
discord9youknowone
authored andcommitted
fix: elide null check in vtable&CR refactor
1 parent ac8582c commit 99e9257

File tree

4 files changed

+16
-36
lines changed

4 files changed

+16
-36
lines changed

vm/src/builtins/tuple.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use super::{PositionIterInternal, PyGenericAlias, PyType, PyTypeRef};
2-
use crate::common::{
3-
hash::PyHash,
4-
lock::{PyMutex, PyRwLock},
5-
};
2+
use crate::common::{hash::PyHash, lock::PyMutex};
63
use crate::{
74
atomic_func,
85
class::PyClassImpl,

vm/src/object/core.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ cfg_if::cfg_if! {
106106

107107
unsafe fn drop_dealloc_obj<T: PyObjectPayload>(x: *mut PyObject) {
108108
#[cfg(feature = "gc_bacon")]
109-
if x.as_ref().unwrap().header().buffered() {
109+
if (*x).header().buffered() {
110110
error!("Try to drop&dealloc a buffered object! Drop only for now!");
111111
drop_only_obj::<T>(x);
112112
} else {
@@ -130,7 +130,7 @@ macro_rules! partially_drop {
130130
/// NOTE: `header` is not drop to prevent UB
131131
#[cfg(feature = "gc_bacon")]
132132
unsafe fn drop_only_obj<T: PyObjectPayload>(x: *mut PyObject) {
133-
let obj = x.cast::<PyInner<T>>().as_ref().expect("Non-Null Pointer");
133+
let obj = &*x.cast::<PyInner<T>>();
134134
partially_drop!(
135135
obj.
136136
#[cfg(debug_assertions)]
@@ -151,12 +151,12 @@ unsafe fn drop_only_obj<T: PyObjectPayload>(x: *mut PyObject) {
151151
#[cfg(feature = "gc_bacon")]
152152
unsafe fn dealloc_only_obj<T: PyObjectPayload>(x: *mut PyObject) {
153153
{
154-
let obj = x.cast::<PyInner<T>>().as_ref().expect("Non-Null Pointer");
154+
let obj = &*x.cast::<PyInner<T>>();
155155
partially_drop!(obj.header, vtable, weak_list);
156156
} // don't want keep a ref to a to be deallocated object
157157
std::alloc::dealloc(
158158
x.cast(),
159-
std::alloc::Layout::for_value(x.cast::<PyInner<T>>().as_ref().unwrap()),
159+
std::alloc::Layout::for_value(&*x.cast::<PyInner<T>>()),
160160
);
161161
}
162162

@@ -165,6 +165,7 @@ unsafe fn debug_obj<T: PyObjectPayload>(x: &PyObject, f: &mut fmt::Formatter) ->
165165
fmt::Debug::fmt(x, f)
166166
}
167167

168+
#[cfg(feature = "gc_bacon")]
168169
unsafe fn try_trace_obj<T: PyObjectPayload>(x: &PyObject, tracer_fn: &mut TracerFn) {
169170
let x = &*(x as *const PyObject as *const PyInner<T>);
170171
let payload = &x.payload;
@@ -185,6 +186,7 @@ impl PyObjVTable {
185186
#[cfg(feature = "gc_bacon")]
186187
dealloc_only: dealloc_only_obj::<T>,
187188
debug: debug_obj::<T>,
189+
#[cfg(feature = "gc_bacon")]
188190
trace: {
189191
if T::IS_TRACE {
190192
Some(try_trace_obj::<T>)
@@ -700,19 +702,17 @@ pub struct PyObject(PyInner<Erased>);
700702

701703
#[cfg(feature = "gc_bacon")]
702704
impl PyObject {
703-
#[cfg(feature = "gc_bacon")]
704-
pub fn header(&self) -> &GcHeader {
705+
pub(in crate::object) fn header(&self) -> &GcHeader {
705706
&self.0.header
706707
}
707708

708-
pub fn inner_typeid(&self) -> TypeId {
709-
self.0.typeid
709+
pub(in crate::object) fn is_traceable(&self) -> bool {
710+
self.0.vtable.trace.is_some()
710711
}
711-
712-
pub fn increment(&self) {
712+
fn increment(&self) {
713713
self.0.header.gc().increment(self)
714714
}
715-
pub fn decrement(&self) -> GcStatus {
715+
fn decrement(&self) -> GcStatus {
716716
self.0.header.gc().decrement(self)
717717
}
718718
}
@@ -1135,23 +1135,6 @@ impl PyObject {
11351135
true
11361136
}
11371137

1138-
#[cfg(feature = "gc_bacon")]
1139-
#[allow(unused)]
1140-
pub(crate) unsafe fn drop_only(ptr: NonNull<PyObject>) -> bool {
1141-
let zelf = ptr.as_ref();
1142-
if !zelf.header().check_set_drop_only() {
1143-
return false;
1144-
}
1145-
1146-
// not set PyInner's is_drop because still havn't dealloc
1147-
let drop_only = zelf.0.vtable.drop_only;
1148-
1149-
drop_only(ptr.as_ptr());
1150-
// Safety: after drop_only, header should still remain undropped
1151-
zelf.header().set_done_drop(true);
1152-
true
1153-
}
1154-
11551138
#[cfg(feature = "gc_bacon")]
11561139
/// run object's __del__ and then rust's destructor but doesn't dealloc
11571140
pub(in crate::object) unsafe fn del_drop(ptr: NonNull<PyObject>) -> bool {

vm/src/object/gc/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
PyObject,
1313
};
1414

15-
use super::{GcObj, GcObjRef, GcStatus, TraceHelper};
15+
use super::{GcObj, GcObjRef, GcStatus};
1616

1717
#[cfg(feature = "threading")]
1818
pub static LOCK_TIMEOUT: Duration = Duration::from_secs(5);
@@ -396,7 +396,7 @@ impl Collector {
396396
let rc = obj.header().dec();
397397
if rc == 0 {
398398
self.release(obj)
399-
} else if TraceHelper::is_traceable(obj.inner_typeid()) && !obj.header().is_leaked() {
399+
} else if obj.is_traceable() && !obj.header().is_leaked() {
400400
// only buffer traceable(and not leaked) object for that is where we can detect cycles
401401
self.possible_root(obj);
402402
GcStatus::ShouldKeep

vm/src/protocol/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ where
1515
O: Borrow<PyObject>;
1616

1717
#[cfg(feature = "gc_bacon")]
18-
unsafe impl<O: crate::object::Trace + Borrow<PyObject>> crate::object::Trace for PyIter<O> {
18+
unsafe impl<O: Borrow<PyObject>> crate::object::Trace for PyIter<O> {
1919
fn trace(&self, tracer_fn: &mut crate::object::TracerFn) {
20-
self.0.trace(tracer_fn);
20+
self.0.borrow().trace(tracer_fn);
2121
}
2222
}
2323

0 commit comments

Comments
 (0)
0