Skip to content

Commit

Permalink
improved rb_eql() for performance, makes faster hash lookup / keys co…
Browse files Browse the repository at this point in the history
…mparison operations

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@5041 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
lrz committed Dec 17, 2010
1 parent a8c1617 commit a4c25b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
26 changes: 26 additions & 0 deletions array.c
Expand Up @@ -2521,6 +2521,32 @@ rary_eql(VALUE ary1, SEL sel, VALUE ary2)
return rb_exec_recursive(recursive_eql, ary1, 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: * call-seq:
* array.include?(obj) -> true or false * array.include?(obj) -> true or false
Expand Down
1 change: 1 addition & 0 deletions array.h
Expand Up @@ -157,6 +157,7 @@ VALUE rary_sort(VALUE ary, SEL sel);
VALUE rary_sort_bang(VALUE ary, SEL sel); VALUE rary_sort_bang(VALUE ary, SEL sel);
VALUE rary_subseq(VALUE ary, long beg, long len); VALUE rary_subseq(VALUE ary, long beg, long len);
void rary_insert(VALUE ary, long idx, VALUE val); void rary_insert(VALUE ary, long idx, VALUE val);
bool rary_eql_fast(rb_ary_t *ary1, rb_ary_t *ary2);


// Shared implementations. // Shared implementations.
VALUE rary_join(VALUE ary, SEL sel, int argc, VALUE *argv); VALUE rary_join(VALUE ary, SEL sel, int argc, VALUE *argv);
Expand Down
1 change: 1 addition & 0 deletions encoding.h
Expand Up @@ -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); 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); void rb_str_NSCoder_encode(void *coder, VALUE str, const char *key);
VALUE rb_str_NSCoder_decode(void *coder, const char *key); VALUE rb_str_NSCoder_decode(void *coder, const char *key);
Expand Down
17 changes: 17 additions & 0 deletions object.c
Expand Up @@ -80,6 +80,23 @@ rb_equal_imp(VALUE obj1, SEL sel, VALUE obj2)
int int
rb_eql(VALUE obj1, VALUE obj2) 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)); return RTEST(rb_vm_call(obj1, eqlSel, 1, &obj2));
} }


Expand Down
6 changes: 6 additions & 0 deletions string.c
Expand Up @@ -918,6 +918,12 @@ str_compare(rb_str_t *self, rb_str_t *str)
return res > 0 ? 1 : -1; return res > 0 ? 1 : -1;
} }


int
rstr_compare(rb_str_t *str1, rb_str_t *str2)
{
return str_compare(str1, str2);
}

static int static int
str_case_compare(rb_str_t *self, rb_str_t *str) str_case_compare(rb_str_t *self, rb_str_t *str)
{ {
Expand Down

0 comments on commit a4c25b8

Please sign in to comment.