Permalink
Browse files

improved rb_eql() for performance, makes faster hash lookup / keys co…

…mparison operations

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@5041 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent a8c1617 commit a4c25b87dff9fcfef258a8044a78975a80d751b4 @lrz lrz committed Dec 17, 2010
Showing with 51 additions and 0 deletions.
  1. +26 −0 array.c
  2. +1 −0 array.h
  3. +1 −0 encoding.h
  4. +17 −0 object.c
  5. +6 −0 string.c
View
26 array.c
@@ -2521,6 +2521,32 @@ rary_eql(VALUE ary1, SEL sel, VALUE ary2)
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
View
1 array.h
@@ -157,6 +157,7 @@ VALUE rary_sort(VALUE ary, SEL sel);
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);
View
1 encoding.h
@@ -328,6 +328,7 @@ str_simple_transcode(rb_str_t *self, rb_encoding_t *dst_encoding)
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);
View
17 object.c
@@ -80,6 +80,23 @@ rb_equal_imp(VALUE obj1, SEL sel, VALUE obj2)
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));
}
View
6 string.c
@@ -918,6 +918,12 @@ str_compare(rb_str_t *self, rb_str_t *str)
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)
{

0 comments on commit a4c25b8

Please sign in to comment.