[macruby-changes] [5041] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Thu Dec 16 20:18:07 PST 2010


Revision: 5041
          http://trac.macosforge.org/projects/ruby/changeset/5041
Author:   lsansonetti at apple.com
Date:     2010-12-16 20:18:01 -0800 (Thu, 16 Dec 2010)
Log Message:
-----------
improved rb_eql() for performance, makes faster hash lookup / keys comparison operations

Modified Paths:
--------------
    MacRuby/trunk/array.c
    MacRuby/trunk/array.h
    MacRuby/trunk/encoding.h
    MacRuby/trunk/object.c
    MacRuby/trunk/string.c

Modified: MacRuby/trunk/array.c
===================================================================
--- MacRuby/trunk/array.c	2010-12-17 01:19:12 UTC (rev 5040)
+++ MacRuby/trunk/array.c	2010-12-17 04:18:01 UTC (rev 5041)
@@ -2521,6 +2521,32 @@
     return rb_exec_recursive(recursive_eql, ary1, ary2);
 }
 
+static VALUE
+recursive_eql_fast(VALUE ary1, VALUE ary2, int recur)
+{
+    if (recur) {
+	return Qfalse;
+    }
+    for (long i = 0; i < RARY(ary1)->len; i++) {
+	if (!rb_eql(rary_elt(ary1, i), rary_elt(ary2, i))) {
+	    return Qfalse;
+	}
+    }
+    return Qtrue;
+}
+
+bool
+rary_eql_fast(rb_ary_t *ary1, rb_ary_t *ary2)
+{
+    if (ary1 == ary2) {
+	return true;
+    }
+    if (ary1->len != ary2->len) {
+	return false;
+    }
+    return rb_exec_recursive(recursive_eql_fast, (VALUE)ary1, (VALUE)ary2);
+}
+
 /*
  *  call-seq:
  *     array.include?(obj)   -> true or false

Modified: MacRuby/trunk/array.h
===================================================================
--- MacRuby/trunk/array.h	2010-12-17 01:19:12 UTC (rev 5040)
+++ MacRuby/trunk/array.h	2010-12-17 04:18:01 UTC (rev 5041)
@@ -157,6 +157,7 @@
 VALUE rary_sort_bang(VALUE ary, SEL sel);
 VALUE rary_subseq(VALUE ary, long beg, long len);
 void rary_insert(VALUE ary, long idx, VALUE val);
+bool rary_eql_fast(rb_ary_t *ary1, rb_ary_t *ary2);
 
 // Shared implementations.
 VALUE rary_join(VALUE ary, SEL sel, int argc, VALUE *argv);

Modified: MacRuby/trunk/encoding.h
===================================================================
--- MacRuby/trunk/encoding.h	2010-12-17 01:19:12 UTC (rev 5040)
+++ MacRuby/trunk/encoding.h	2010-12-17 04:18:01 UTC (rev 5041)
@@ -328,6 +328,7 @@
         TRANSCODE_BEHAVIOR_RAISE_EXCEPTION, TRANSCODE_BEHAVIOR_RAISE_EXCEPTION, NULL);
 }
 
+int rstr_compare(rb_str_t *str1, rb_str_t *str2);
 
 void rb_str_NSCoder_encode(void *coder, VALUE str, const char *key);
 VALUE rb_str_NSCoder_decode(void *coder, const char *key);

Modified: MacRuby/trunk/object.c
===================================================================
--- MacRuby/trunk/object.c	2010-12-17 01:19:12 UTC (rev 5040)
+++ MacRuby/trunk/object.c	2010-12-17 04:18:01 UTC (rev 5041)
@@ -80,6 +80,23 @@
 int
 rb_eql(VALUE obj1, VALUE obj2)
 {
+    VALUE obj1_class = CLASS_OF(obj1);
+
+    if (obj1_class == rb_cFixnum || obj1_class == rb_cFloat
+	    || obj1_class == rb_cSymbol) {
+	return obj1 == obj2;
+    }
+
+    VALUE obj2_class = CLASS_OF(obj2);
+    if (obj1_class == obj2_class) {
+	if (obj1_class == rb_cRubyString) {
+	    return rstr_compare(RSTR(obj1), RSTR(obj2)) == 0;
+	}
+	if (obj1_class == rb_cRubyArray) {
+	    return rary_eql_fast(RARY(obj1), RARY(obj2));
+	}
+    }
+
     return RTEST(rb_vm_call(obj1, eqlSel, 1, &obj2));
 }
 

Modified: MacRuby/trunk/string.c
===================================================================
--- MacRuby/trunk/string.c	2010-12-17 01:19:12 UTC (rev 5040)
+++ MacRuby/trunk/string.c	2010-12-17 04:18:01 UTC (rev 5041)
@@ -918,6 +918,12 @@
     return res > 0 ? 1 : -1;
 }
 
+int
+rstr_compare(rb_str_t *str1, rb_str_t *str2)
+{
+    return str_compare(str1, str2);
+}
+
 static int
 str_case_compare(rb_str_t *self, rb_str_t *str)
 {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101216/ebf6f3ab/attachment-0001.html>


More information about the macruby-changes mailing list