Skip to content

Commit d10da60

Browse files
committed
Optimize as_class_entry to avoid looking up every time
1 parent 02be0de commit d10da60

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

phper/src/classes.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::{
3030
marker::PhantomData,
3131
mem::{ManuallyDrop, replace, size_of, transmute, zeroed},
3232
os::raw::c_int,
33-
ptr::{self, null_mut},
33+
ptr::{self, null, null_mut},
3434
rc::Rc,
3535
slice,
3636
};
@@ -234,7 +234,7 @@ fn find_global_class_entry_ptr(name: impl AsRef<str>) -> *mut zend_class_entry {
234234

235235
#[derive(Clone)]
236236
enum InnerClassEntry {
237-
Ptr(Rc<RefCell<*mut zend_class_entry>>),
237+
Ptr(*const zend_class_entry),
238238
Name(String),
239239
}
240240

@@ -279,15 +279,15 @@ enum InnerClassEntry {
279279
/// }
280280
/// ```
281281
pub struct StateClass<T> {
282-
inner: InnerClassEntry,
282+
inner: Rc<RefCell<InnerClassEntry>>,
283283
_p: PhantomData<T>,
284284
}
285285

286286
impl StateClass<()> {
287287
/// Create from name, which will be looked up from globals.
288288
pub fn from_name(name: impl Into<String>) -> Self {
289289
Self {
290-
inner: InnerClassEntry::Name(name.into()),
290+
inner: Rc::new(RefCell::new(InnerClassEntry::Name(name.into()))),
291291
_p: PhantomData,
292292
}
293293
}
@@ -296,15 +296,15 @@ impl StateClass<()> {
296296
impl<T> StateClass<T> {
297297
fn null() -> Self {
298298
Self {
299-
inner: InnerClassEntry::Ptr(Rc::new(RefCell::new(null_mut()))),
299+
inner: Rc::new(RefCell::new(InnerClassEntry::Ptr(null()))),
300300
_p: PhantomData,
301301
}
302302
}
303303

304304
fn bind(&self, ptr: *mut zend_class_entry) {
305-
match &self.inner {
305+
match &mut *self.inner.borrow_mut() {
306306
InnerClassEntry::Ptr(p) => {
307-
*p.borrow_mut() = ptr;
307+
*p = ptr;
308308
}
309309
InnerClassEntry::Name(_) => {
310310
unreachable!("Cannot bind() an StateClass created with from_name()");
@@ -314,9 +314,13 @@ impl<T> StateClass<T> {
314314

315315
/// Converts to class entry.
316316
pub fn as_class_entry(&self) -> &ClassEntry {
317-
match &self.inner {
318-
InnerClassEntry::Ptr(ptr) => unsafe { ClassEntry::from_mut_ptr(*ptr.borrow()) },
319-
InnerClassEntry::Name(name) => ClassEntry::from_globals(name).unwrap(),
317+
match self.inner.borrow().clone() {
318+
InnerClassEntry::Ptr(ptr) => unsafe { ClassEntry::from_ptr(ptr) },
319+
InnerClassEntry::Name(name) => {
320+
let entry = ClassEntry::from_globals(name).unwrap();
321+
*self.inner.borrow_mut() = InnerClassEntry::Ptr(entry.as_ptr());
322+
entry
323+
}
320324
}
321325
}
322326

@@ -388,27 +392,27 @@ impl<T> Clone for StateClass<T> {
388392
/// ```
389393
#[derive(Clone)]
390394
pub struct Interface {
391-
inner: InnerClassEntry,
395+
inner: Rc<RefCell<InnerClassEntry>>,
392396
}
393397

394398
impl Interface {
395399
fn null() -> Self {
396400
Self {
397-
inner: InnerClassEntry::Ptr(Rc::new(RefCell::new(null_mut()))),
401+
inner: Rc::new(RefCell::new(InnerClassEntry::Ptr(null()))),
398402
}
399403
}
400404

401405
/// Create a new interface from global name (eg "Stringable", "ArrayAccess")
402406
pub fn from_name(name: impl Into<String>) -> Self {
403407
Self {
404-
inner: InnerClassEntry::Name(name.into()),
408+
inner: Rc::new(RefCell::new(InnerClassEntry::Name(name.into()))),
405409
}
406410
}
407411

408412
fn bind(&self, ptr: *mut zend_class_entry) {
409-
match &self.inner {
413+
match &mut *self.inner.borrow_mut() {
410414
InnerClassEntry::Ptr(p) => {
411-
*p.borrow_mut() = ptr;
415+
*p = ptr;
412416
}
413417
InnerClassEntry::Name(_) => {
414418
unreachable!("Cannot bind() an Interface created with from_name()");
@@ -418,9 +422,13 @@ impl Interface {
418422

419423
/// Converts to class entry.
420424
pub fn as_class_entry(&self) -> &ClassEntry {
421-
match &self.inner {
422-
InnerClassEntry::Ptr(ptr) => unsafe { ClassEntry::from_mut_ptr(*ptr.borrow()) },
423-
InnerClassEntry::Name(name) => ClassEntry::from_globals(name).unwrap(),
425+
match self.inner.borrow().clone() {
426+
InnerClassEntry::Ptr(ptr) => unsafe { ClassEntry::from_ptr(ptr) },
427+
InnerClassEntry::Name(name) => {
428+
let entry = ClassEntry::from_globals(name).unwrap();
429+
*self.inner.borrow_mut() = InnerClassEntry::Ptr(entry.as_ptr());
430+
entry
431+
}
424432
}
425433
}
426434
}

0 commit comments

Comments
 (0)