a = [:e, :f] 和 a.replace [:e, :f],
这两条语句生成的指令如下:
1.
a = [:a, :b, :c, :d]
a = [:e, :f]
指示:
红宝石--dump=insns test.rb
== disasm: <RubyVM::InstructionSequence:<main>@test.rb>=================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 2] a
0000 trace 1 ( 1)
0002 duparray [:a, :b, :c, :d]
0004 setdynamic a, 0
0007 trace 1 ( 2)
0009 duparray [:e, :f]
0011 dup
0012 setdynamic a, 0
0015 leave
2.
a = [:a, :b, :c, :d]
a.replace([:e, :f])
指示:
红宝石--dump=insns test.rb
== disasm: <RubyVM::InstructionSequence:<main>@test.rb>=================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
[ 2] a
0000 trace 1 ( 1)
0002 duparray [:a, :b, :c, :d]
0004 setdynamic a, 0
0007 trace 1 ( 2)
0009 getdynamic a, 0
0012 duparray [:e, :f]
0014 send :replace, 1, nil, 0, <ic:0>
0020 leave
replace方法并不比赋值运算符快,但是replace可以就地修改接收器数组,而且,replace方法确实节省了内存,这可以从 rb_ary_replace 的源码中看出。
VALUE
rb_ary_replace(VALUE copy, VALUE orig)
{
rb_ary_modify_check(copy);
orig = to_ary(orig);
if (copy == orig) return copy;
if (RARRAY_LEN(orig) <= RARRAY_EMBED_LEN_MAX)
{
VALUE *ptr;
VALUE shared = 0;
if (ARY_OWNS_HEAP_P(copy))
{
xfree(RARRAY_PTR(copy));
}
else if (ARY_SHARED_P(copy))
{
shared = ARY_SHARED(copy);
FL_UNSET_SHARED(copy);
}
FL_SET_EMBED(copy);
ptr = RARRAY_PTR(orig);
MEMCPY(RARRAY_PTR(copy), ptr, VALUE, RARRAY_LEN(orig));
if (shared)
{
rb_ary_decrement_share(shared);
}
ARY_SET_LEN(copy, RARRAY_LEN(orig));
}
else
{
VALUE shared = ary_make_shared(orig);
if (ARY_OWNS_HEAP_P(copy))
{
xfree(RARRAY_PTR(copy));
}
else
{
rb_ary_unshare_safe(copy);
}
FL_UNSET_EMBED(copy);
ARY_SET_PTR(copy, RARRAY_PTR(orig));
ARY_SET_LEN(copy, RARRAY_LEN(orig));
rb_ary_set_shared(copy, shared);
}
return copy; }