Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit a4c25b87dff9fcfef258a8044a78975a80d751b4 1 parent a8c1617
Laurent Sansonetti authored

Showing 5 changed files with 51 additions and 0 deletions. Show diff stats Hide diff stats

  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
26 array.c
@@ -2521,6 +2521,32 @@ rary_eql(VALUE ary1, SEL sel, VALUE ary2)
2521 2521 return rb_exec_recursive(recursive_eql, ary1, ary2);
2522 2522 }
2523 2523
  2524 +static VALUE
  2525 +recursive_eql_fast(VALUE ary1, VALUE ary2, int recur)
  2526 +{
  2527 + if (recur) {
  2528 + return Qfalse;
  2529 + }
  2530 + for (long i = 0; i < RARY(ary1)->len; i++) {
  2531 + if (!rb_eql(rary_elt(ary1, i), rary_elt(ary2, i))) {
  2532 + return Qfalse;
  2533 + }
  2534 + }
  2535 + return Qtrue;
  2536 +}
  2537 +
  2538 +bool
  2539 +rary_eql_fast(rb_ary_t *ary1, rb_ary_t *ary2)
  2540 +{
  2541 + if (ary1 == ary2) {
  2542 + return true;
  2543 + }
  2544 + if (ary1->len != ary2->len) {
  2545 + return false;
  2546 + }
  2547 + return rb_exec_recursive(recursive_eql_fast, (VALUE)ary1, (VALUE)ary2);
  2548 +}
  2549 +
2524 2550 /*
2525 2551 * call-seq:
2526 2552 * array.include?(obj) -> true or false
1  array.h
@@ -157,6 +157,7 @@ VALUE rary_sort(VALUE ary, SEL sel);
157 157 VALUE rary_sort_bang(VALUE ary, SEL sel);
158 158 VALUE rary_subseq(VALUE ary, long beg, long len);
159 159 void rary_insert(VALUE ary, long idx, VALUE val);
  160 +bool rary_eql_fast(rb_ary_t *ary1, rb_ary_t *ary2);
160 161
161 162 // Shared implementations.
162 163 VALUE rary_join(VALUE ary, SEL sel, int argc, VALUE *argv);
1  encoding.h
@@ -328,6 +328,7 @@ str_simple_transcode(rb_str_t *self, rb_encoding_t *dst_encoding)
328 328 TRANSCODE_BEHAVIOR_RAISE_EXCEPTION, TRANSCODE_BEHAVIOR_RAISE_EXCEPTION, NULL);
329 329 }
330 330
  331 +int rstr_compare(rb_str_t *str1, rb_str_t *str2);
331 332
332 333 void rb_str_NSCoder_encode(void *coder, VALUE str, const char *key);
333 334 VALUE rb_str_NSCoder_decode(void *coder, const char *key);
17 object.c
@@ -80,6 +80,23 @@ rb_equal_imp(VALUE obj1, SEL sel, VALUE obj2)
80 80 int
81 81 rb_eql(VALUE obj1, VALUE obj2)
82 82 {
  83 + VALUE obj1_class = CLASS_OF(obj1);
  84 +
  85 + if (obj1_class == rb_cFixnum || obj1_class == rb_cFloat
  86 + || obj1_class == rb_cSymbol) {
  87 + return obj1 == obj2;
  88 + }
  89 +
  90 + VALUE obj2_class = CLASS_OF(obj2);
  91 + if (obj1_class == obj2_class) {
  92 + if (obj1_class == rb_cRubyString) {
  93 + return rstr_compare(RSTR(obj1), RSTR(obj2)) == 0;
  94 + }
  95 + if (obj1_class == rb_cRubyArray) {
  96 + return rary_eql_fast(RARY(obj1), RARY(obj2));
  97 + }
  98 + }
  99 +
83 100 return RTEST(rb_vm_call(obj1, eqlSel, 1, &obj2));
84 101 }
85 102
6 string.c
@@ -918,6 +918,12 @@ str_compare(rb_str_t *self, rb_str_t *str)
918 918 return res > 0 ? 1 : -1;
919 919 }
920 920
  921 +int
  922 +rstr_compare(rb_str_t *str1, rb_str_t *str2)
  923 +{
  924 + return str_compare(str1, str2);
  925 +}
  926 +
921 927 static int
922 928 str_case_compare(rb_str_t *self, rb_str_t *str)
923 929 {

0 comments on commit a4c25b8

Please sign in to comment.
Something went wrong with that request. Please try again.