ruby-changes:48218
From: nobu <ko1@a...>
Date: Sun, 22 Oct 2017 09:19:17 +0900 (JST)
Subject: [ruby-changes:48218] nobu:r60333 (trunk): vm_insnhelper.c: array aref optimization
nobu 2017-10-22 09:19:12 +0900 (Sun, 22 Oct 2017) New Revision: 60333 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60333 Log: vm_insnhelper.c: array aref optimization * vm_insnhelper.c (vm_opt_aref): optimize on other than fixnum argument too. Modified files: trunk/array.c trunk/internal.h trunk/vm_insnhelper.c Index: internal.h =================================================================== --- internal.h (revision 60332) +++ internal.h (revision 60333) @@ -1032,6 +1032,8 @@ void rb_ary_set_len(VALUE, long); https://github.com/ruby/ruby/blob/trunk/internal.h#L1032 void rb_ary_delete_same(VALUE, VALUE); VALUE rb_ary_tmp_new_fill(long capa); VALUE rb_ary_at(VALUE, VALUE); +VALUE rb_ary_aref1(VALUE ary, VALUE i); +VALUE rb_ary_aref2(VALUE ary, VALUE b, VALUE e); size_t rb_ary_memsize(VALUE); #ifdef __GNUC__ #define rb_ary_new_from_args(n, ...) \ Index: vm_insnhelper.c =================================================================== --- vm_insnhelper.c (revision 60332) +++ vm_insnhelper.c (revision 60333) @@ -3584,9 +3584,8 @@ vm_opt_aref(VALUE recv, VALUE obj) https://github.com/ruby/ruby/blob/trunk/vm_insnhelper.c#L3584 return Qundef; } else if (RBASIC_CLASS(recv) == rb_cArray && - BASIC_OP_UNREDEFINED_P(BOP_AREF, ARRAY_REDEFINED_OP_FLAG) && - FIXNUM_P(obj)) { - return rb_ary_entry(recv, FIX2LONG(obj)); + BASIC_OP_UNREDEFINED_P(BOP_AREF, ARRAY_REDEFINED_OP_FLAG)) { + return rb_ary_aref1(recv, obj); } else if (RBASIC_CLASS(recv) == rb_cHash && BASIC_OP_UNREDEFINED_P(BOP_AREF, HASH_REDEFINED_OP_FLAG)) { Index: array.c =================================================================== --- array.c (revision 60332) +++ array.c (revision 60333) @@ -1284,21 +1284,29 @@ rb_ary_subseq(VALUE ary, long beg, long https://github.com/ruby/ruby/blob/trunk/array.c#L1284 VALUE rb_ary_aref(int argc, const VALUE *argv, VALUE ary) { - VALUE arg; - long beg, len; - + rb_check_arity(argc, 1, 2); if (argc == 2) { - beg = NUM2LONG(argv[0]); - len = NUM2LONG(argv[1]); - if (beg < 0) { - beg += RARRAY_LEN(ary); - } - return rb_ary_subseq(ary, beg, len); + return rb_ary_aref2(ary, argv[0], argv[1]); } - if (argc != 1) { - rb_scan_args(argc, argv, "11", NULL, NULL); + return rb_ary_aref1(ary, argv[0]); +} + +VALUE +rb_ary_aref2(VALUE ary, VALUE b, VALUE e) +{ + long beg = NUM2LONG(b); + long len = NUM2LONG(e); + if (beg < 0) { + beg += RARRAY_LEN(ary); } - arg = argv[0]; + return rb_ary_subseq(ary, beg, len); +} + +VALUE +rb_ary_aref1(VALUE ary, VALUE arg) +{ + long beg, len; + /* special case - speeding up */ if (FIXNUM_P(arg)) { return rb_ary_entry(ary, FIX2LONG(arg)); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/