From 48bfe11f8f67280d8d151f452705c4ae73ac0b7f Mon Sep 17 00:00:00 2001 From: Alastair Pharo Date: Sat, 31 Aug 2019 13:34:29 +1000 Subject: [PATCH] Add faster versions of `equals` and `is_equal` - `Object::equals` is now a wrapper around the `rb_equal` C function; - `Object::is_equal` simply performs an `==` comparison on the underlying values, which is the same behaviour as the `rb_obj_equal` C function. --- src/binding/class.rs | 4 ++++ src/class/traits/object.rs | 14 +++----------- src/rubysys/class.rs | 3 +++ 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/binding/class.rs b/src/binding/class.rs index 367923ec..fb7afd5c 100644 --- a/src/binding/class.rs +++ b/src/binding/class.rs @@ -122,3 +122,7 @@ pub fn is_frozen(object: Value) -> Value { pub fn freeze(object: Value) -> Value { unsafe { class::rb_obj_freeze(object) } } + +pub fn equals(object1: Value, object2: Value) -> Value { + unsafe { class::rb_equal(object1, object2) } +} diff --git a/src/class/traits/object.rs b/src/class/traits/object.rs index ef652729..d49c27c9 100644 --- a/src/class/traits/object.rs +++ b/src/class/traits/object.rs @@ -745,11 +745,7 @@ pub trait Object: From { /// a == c # true /// ``` fn equals(&self, other: &T) -> bool { - let v = self.value(); - let m = "=="; - let a = [other.value()]; - - vm::call_method(v, m, &a).is_true() + class::equals(self.value(), other.value()).is_true() } /// Alias for Ruby's `===` @@ -843,14 +839,10 @@ pub trait Object: From { /// /// /// a.equal?(b) - /// a.eqlua?(c) + /// a.equal?(c) /// ``` fn is_equal(&self, other: &T) -> bool { - let v = self.value(); - let m = "equal?"; - let a = [other.value()]; - - vm::call_method(v, m, &a).is_true() + self.value() == other.value() } /// Checks whether the object responds to given method diff --git a/src/rubysys/class.rs b/src/rubysys/class.rs index 84816cf3..fec8f404 100644 --- a/src/rubysys/class.rs +++ b/src/rubysys/class.rs @@ -43,6 +43,9 @@ extern "C" { name: *const c_char, callback: CallbackPtr, argc: Argc); + // VALUE + // rb_equal(VALUE obj1, VALUE obj2) + pub fn rb_equal(obj1: Value, obj2: Value) -> Value; // void // rb_extend_object(VALUE object, VALUE module) pub fn rb_extend_object(object: Value, module: Value);