Skip to content

Commit 0ebb7de

Browse files
committed
Make PyObjectRef = Arc<...>
1 parent 90d184c commit 0ebb7de

File tree

6 files changed

+31
-33
lines changed

6 files changed

+31
-33
lines changed

vm/src/obj/objweakref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::pyobject::{
66
use crate::slots::SlotCall;
77
use crate::vm::VirtualMachine;
88

9-
use std::rc::{Rc, Weak};
9+
use std::sync::{Arc, Weak};
1010

1111
#[pyclass]
1212
#[derive(Debug)]
@@ -17,7 +17,7 @@ pub struct PyWeak {
1717
impl PyWeak {
1818
pub fn downgrade(obj: &PyObjectRef) -> PyWeak {
1919
PyWeak {
20-
referent: Rc::downgrade(obj),
20+
referent: Arc::downgrade(obj),
2121
}
2222
}
2323

vm/src/pyobject.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use std::collections::HashMap;
44
use std::fmt;
55
use std::marker::PhantomData;
66
use std::ops::Deref;
7-
use std::rc::Rc;
8-
use std::sync::Mutex;
7+
use std::sync::{Arc, Mutex};
98

109
use indexmap::IndexMap;
1110
use num_bigint::BigInt;
@@ -62,7 +61,7 @@ Basically reference counting, but then done by rust.
6261
/// this reference counting is accounted for by this type. Use the `.clone()`
6362
/// method to create a new reference and increment the amount of references
6463
/// to the python object by 1.
65-
pub type PyObjectRef = Rc<PyObject<dyn PyObjectPayload>>;
64+
pub type PyObjectRef = Arc<PyObject<dyn PyObjectPayload>>;
6665

6766
/// Use this type for functions which return a python object or an exception.
6867
/// Both the python object and the python exception are `PyObjectRef` types
@@ -629,7 +628,7 @@ pub struct PyObject<T>
629628
where
630629
T: ?Sized + PyObjectPayload,
631630
{
632-
pub typ: Rc<PyObject<PyClass>>,
631+
pub typ: Arc<PyObject<PyClass>>,
633632
// TODO: make this RwLock once PyObjectRef is Send + Sync
634633
pub(crate) dict: Option<Mutex<PyDictRef>>, // __dict__ member
635634
pub payload: T,
@@ -640,7 +639,7 @@ impl PyObject<dyn PyObjectPayload> {
640639
///
641640
/// If the downcast fails, the original ref is returned in as `Err` so
642641
/// another downcast can be attempted without unnecessary cloning.
643-
pub fn downcast<T: PyObjectPayload>(self: Rc<Self>) -> Result<PyRef<T>, PyObjectRef> {
642+
pub fn downcast<T: PyObjectPayload>(self: Arc<Self>) -> Result<PyRef<T>, PyObjectRef> {
644643
if self.payload_is::<T>() {
645644
Ok(PyRef {
646645
obj: self,
@@ -651,15 +650,15 @@ impl PyObject<dyn PyObjectPayload> {
651650
}
652651
}
653652

654-
/// Downcast this PyObjectRef to an `Rc<PyObject<T>>`. The [`downcast`](#method.downcast) method
653+
/// Downcast this PyObjectRef to an `Arc<PyObject<T>>`. The [`downcast`](#method.downcast) method
655654
/// is generally preferred, as the `PyRef<T>` it returns implements `Deref<Target=T>`, and
656655
/// therefore can be used similarly to an `&T`.
657656
pub fn downcast_generic<T: PyObjectPayload>(
658-
self: Rc<Self>,
659-
) -> Result<Rc<PyObject<T>>, PyObjectRef> {
657+
self: Arc<Self>,
658+
) -> Result<Arc<PyObject<T>>, PyObjectRef> {
660659
if self.payload_is::<T>() {
661-
let ptr = Rc::into_raw(self) as *const PyObject<T>;
662-
let ret = unsafe { Rc::from_raw(ptr) };
660+
let ptr = Arc::into_raw(self) as *const PyObject<T>;
661+
let ret = unsafe { Arc::from_raw(ptr) };
663662
Ok(ret)
664663
} else {
665664
Err(self)
@@ -724,7 +723,7 @@ impl<T: PyValue> PyRef<T> {
724723
self.obj.class()
725724
}
726725

727-
pub fn into_typed_pyobj(self) -> Rc<PyObject<T>> {
726+
pub fn into_typed_pyobj(self) -> Arc<PyObject<T>> {
728727
self.into_object().downcast_generic().unwrap()
729728
}
730729
}
@@ -833,7 +832,7 @@ impl<T: ?Sized + PyObjectPayload> IdProtocol for PyObject<T> {
833832
}
834833
}
835834

836-
impl<T: ?Sized + IdProtocol> IdProtocol for Rc<T> {
835+
impl<T: ?Sized + IdProtocol> IdProtocol for Arc<T> {
837836
fn get_id(&self) -> usize {
838837
(**self).get_id()
839838
}
@@ -1141,10 +1140,10 @@ where
11411140

11421141
// Move this object into a reference object, transferring ownership.
11431142
pub fn into_ref(self) -> PyObjectRef {
1144-
Rc::new(self)
1143+
Arc::new(self)
11451144
}
11461145

1147-
pub fn into_pyref(self: Rc<Self>) -> PyRef<T>
1146+
pub fn into_pyref(self: Arc<Self>) -> PyRef<T>
11481147
where
11491148
T: PyValue,
11501149
{

vm/src/stdlib/weakref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
88
use crate::pyobject::PyObjectRef;
99
use crate::vm::VirtualMachine;
10-
use std::rc::Rc;
10+
use std::sync::Arc;
1111

1212
fn weakref_getweakrefcount(obj: PyObjectRef) -> usize {
13-
Rc::weak_count(&obj)
13+
Arc::weak_count(&obj)
1414
}
1515

1616
fn weakref_getweakrefs(_obj: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {

vm/src/sysmodule.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::rc::Rc;
1+
use std::sync::Arc;
22
use std::{env, mem};
33

44
use crate::frame::FrameRef;
@@ -99,7 +99,7 @@ impl SysFlags {
9999
}
100100

101101
fn sys_getrefcount(obj: PyObjectRef) -> usize {
102-
Rc::strong_count(&obj)
102+
Arc::strong_count(&obj)
103103
}
104104

105105
fn sys_getsizeof(obj: PyObjectRef) -> usize {

vm/src/types.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::pyobject::{PyAttributes, PyContext, PyObject};
4343
use std::cell::RefCell;
4444
use std::mem::MaybeUninit;
4545
use std::ptr;
46-
use std::rc::Rc;
46+
use std::sync::Arc;
4747

4848
/// Holder of references to builtin types.
4949
#[derive(Debug)]
@@ -294,9 +294,9 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
294294
// (and yes, this will never get dropped. TODO?)
295295
let (type_type, object_type) = unsafe {
296296
type PyClassObj = PyObject<PyClass>;
297-
type UninitRef<T> = Rc<MaybeUninit<T>>;
297+
type UninitRef<T> = Arc<MaybeUninit<T>>;
298298

299-
let type_type: UninitRef<PyClassObj> = Rc::new(partially_init!(
299+
let type_type: UninitRef<PyClassObj> = Arc::new(partially_init!(
300300
PyObject::<PyClass> {
301301
dict: None,
302302
payload: PyClass {
@@ -310,7 +310,7 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
310310
},
311311
Uninit { typ }
312312
));
313-
let object_type: UninitRef<PyClassObj> = Rc::new(partially_init!(
313+
let object_type: UninitRef<PyClassObj> = Arc::new(partially_init!(
314314
PyObject::<PyClass> {
315315
dict: None,
316316
payload: PyClass {
@@ -326,21 +326,21 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
326326
));
327327

328328
let object_type_ptr =
329-
Rc::into_raw(object_type) as *mut MaybeUninit<PyClassObj> as *mut PyClassObj;
329+
Arc::into_raw(object_type) as *mut MaybeUninit<PyClassObj> as *mut PyClassObj;
330330
let type_type_ptr =
331-
Rc::into_raw(type_type.clone()) as *mut MaybeUninit<PyClassObj> as *mut PyClassObj;
331+
Arc::into_raw(type_type.clone()) as *mut MaybeUninit<PyClassObj> as *mut PyClassObj;
332332

333333
ptr::write(
334-
&mut (*object_type_ptr).typ as *mut Rc<PyClassObj> as *mut UninitRef<PyClassObj>,
334+
&mut (*object_type_ptr).typ as *mut Arc<PyClassObj> as *mut UninitRef<PyClassObj>,
335335
type_type.clone(),
336336
);
337337
ptr::write(
338-
&mut (*type_type_ptr).typ as *mut Rc<PyClassObj> as *mut UninitRef<PyClassObj>,
338+
&mut (*type_type_ptr).typ as *mut Arc<PyClassObj> as *mut UninitRef<PyClassObj>,
339339
type_type,
340340
);
341341

342-
let type_type = PyClassRef::new_ref_unchecked(Rc::from_raw(type_type_ptr));
343-
let object_type = PyClassRef::new_ref_unchecked(Rc::from_raw(object_type_ptr));
342+
let type_type = PyClassRef::new_ref_unchecked(Arc::from_raw(type_type_ptr));
343+
let object_type = PyClassRef::new_ref_unchecked(Arc::from_raw(object_type_ptr));
344344

345345
(*type_type_ptr).payload.mro = vec![object_type.clone()];
346346
(*type_type_ptr).payload.bases = vec![object_type.clone()];

vm/src/vm.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use std::borrow::Borrow;
88
use std::cell::{Cell, Ref, RefCell};
99
use std::collections::hash_map::HashMap;
1010
use std::collections::hash_set::HashSet;
11-
use std::rc::Rc;
12-
use std::sync::{Mutex, MutexGuard};
11+
use std::sync::{Arc, Mutex, MutexGuard};
1312
use std::{env, fmt};
1413

1514
use arr_macro::arr;
@@ -681,7 +680,7 @@ impl VirtualMachine {
681680
pub fn isinstance(&self, obj: &PyObjectRef, cls: &PyClassRef) -> PyResult<bool> {
682681
// cpython first does an exact check on the type, although documentation doesn't state that
683682
// https://github.com/python/cpython/blob/a24107b04c1277e3c1105f98aff5bfa3a98b33a0/Objects/abstract.c#L2408
684-
if Rc::ptr_eq(&obj.class().into_object(), cls.as_object()) {
683+
if Arc::ptr_eq(&obj.class().into_object(), cls.as_object()) {
685684
Ok(true)
686685
} else {
687686
let ret = self.call_method(cls.as_object(), "__instancecheck__", vec![obj.clone()])?;

0 commit comments

Comments
 (0)