summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-08-27 07:46:08 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-08-27 07:46:08 +0000
commitf25daa254146c782ca50e6fc707f8f132e896e93 (patch)
treed6179b6f023850d510f481b28f0eae3b49087a48 /array.c
parentf01cef2050bdc52f580897aaae15564ee01ff2b0 (diff)
array.c: optimized equality
* array.c (rb_ary_index, rb_ary_rindex): use optimized equality to improve performance. [Feature #8820] * vm_insnhelper.c (rb_equal_opt): optimized equality function. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42704 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c42
1 files changed, 33 insertions, 9 deletions
diff --git a/array.c b/array.c
index 5a8d8dc..651f45b 100644
--- a/array.c
+++ b/array.c
@@ -1414,8 +1414,9 @@ rb_ary_fetch(int argc, VALUE *argv, VALUE ary)
static VALUE
rb_ary_index(int argc, VALUE *argv, VALUE ary)
{
+ const VALUE *ptr;
VALUE val;
- long i;
+ long i, len;
if (argc == 0) {
RETURN_ENUMERATOR(ary, 0, 0);
@@ -1426,12 +1427,24 @@ rb_ary_index(int argc, VALUE *argv, VALUE ary)
}
return Qnil;
}
- rb_scan_args(argc, argv, "1", &val);
+ rb_check_arity(argc, 0, 1);
+ val = argv[0];
if (rb_block_given_p())
rb_warn("given block not used");
- for (i=0; i<RARRAY_LEN(ary); i++) {
- if (rb_equal(RARRAY_AREF(ary, i), val))
+ len = RARRAY_LEN(ary);
+ ptr = RARRAY_RAWPTR(ary);
+ for (i=0; i<len; i++) {
+ VALUE e = ptr[i];
+ switch (rb_equal_opt(e, val)) {
+ case Qundef:
+ if (!rb_equal(e, val)) break;
+ case Qtrue:
return LONG2NUM(i);
+ case Qfalse:
+ continue;
+ }
+ len = RARRAY_LEN(ary);
+ ptr = RARRAY_RAWPTR(ary);
}
return Qnil;
}
@@ -1463,8 +1476,9 @@ rb_ary_index(int argc, VALUE *argv, VALUE ary)
static VALUE
rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
{
+ const VALUE *ptr;
VALUE val;
- long i = RARRAY_LEN(ary);
+ long i = RARRAY_LEN(ary), len;
if (argc == 0) {
RETURN_ENUMERATOR(ary, 0, 0);
@@ -1477,15 +1491,25 @@ rb_ary_rindex(int argc, VALUE *argv, VALUE ary)
}
return Qnil;
}
- rb_scan_args(argc, argv, "1", &val);
+ rb_check_arity(argc, 0, 1);
+ val = argv[0];
if (rb_block_given_p())
rb_warn("given block not used");
+ ptr = RARRAY_RAWPTR(ary);
while (i--) {
- if (rb_equal(RARRAY_AREF(ary, i), val))
+ VALUE e = ptr[i];
+ switch (rb_equal_opt(e, val)) {
+ case Qundef:
+ if (!rb_equal(e, val)) break;
+ case Qtrue:
return LONG2NUM(i);
- if (i > RARRAY_LEN(ary)) {
- i = RARRAY_LEN(ary);
+ case Qfalse:
+ continue;
}
+ if (i > (len = RARRAY_LEN(ary))) {
+ i = len;
+ }
+ ptr = RARRAY_RAWPTR(ary);
}
return Qnil;
}