Skip to content

Commit 7c2856d

Browse files
committed
Make dict repr more efficient
1 parent c501103 commit 7c2856d

4 files changed

Lines changed: 304 additions & 221 deletions

File tree

common/src/cell.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,31 @@ cfg_if::cfg_if! {
119119
type RwLockInner<T> = RefCell<T>;
120120
type RwLockReadInner<'a, T> = Ref<'a, T>;
121121
type MappedRwLockReadInner<'a, T> = Ref<'a, T>;
122-
type RwLockWriteInner<'a, T> = RefMut<'a, T>;
122+
#[derive(Debug)]
123+
struct RwLockWriteInner<'a, T: ?Sized>(&'a RefCell<T>, RefMut<'a, T>);
124+
impl<'a, T: ?Sized> RwLockWriteInner<'a, T> {
125+
fn map<U: ?Sized, F>(s: Self, f: F) -> MappedRwLockWriteInner<'a, U>
126+
where
127+
F: FnOnce(&mut T) -> &mut U,
128+
{
129+
RefMut::map(s.1, f)
130+
}
131+
fn downgrade(s: Self) -> RwLockReadInner<'a, T> {
132+
drop(s.1);
133+
read_rwlock(s.0)
134+
}
135+
}
136+
impl<'a, T: ?Sized> Deref for RwLockWriteInner<'a, T> {
137+
type Target = T;
138+
fn deref(&self) -> &T {
139+
self.1.deref()
140+
}
141+
}
142+
impl<'a, T: ?Sized> DerefMut for RwLockWriteInner<'a, T> {
143+
fn deref_mut(&mut self) -> &mut T {
144+
self.1.deref_mut()
145+
}
146+
}
123147
type MappedRwLockWriteInner<'a, T> = RefMut<'a, T>;
124148
const fn new_rwlock<T>(value: T) -> RwLockInner<T> {
125149
RefCell::new(value)
@@ -128,7 +152,7 @@ cfg_if::cfg_if! {
128152
m.borrow()
129153
}
130154
fn write_rwlock<T: ?Sized>(m: &RwLockInner<T>) -> RwLockWriteInner<T> {
131-
m.borrow_mut()
155+
RwLockWriteInner(m, m.borrow_mut())
132156
}
133157
}
134158
}
@@ -229,6 +253,9 @@ impl<'a, T: ?Sized> PyRwLockWriteGuard<'a, T> {
229253
{
230254
PyMappedRwLockWriteGuard(RwLockWriteInner::map(s.0, f))
231255
}
256+
pub fn downgrade(s: Self) -> PyRwLockReadGuard<'a, T> {
257+
PyRwLockReadGuard(RwLockWriteInner::downgrade(s.0))
258+
}
232259
}
233260
impl<'a, T: ?Sized> PyMappedRwLockWriteGuard<'a, T> {
234261
#[inline]

0 commit comments

Comments
 (0)