Thursday, April 12, 2012

What are the uses of ruby's Hash.replace or Array.replace?

I always see replace in the Array and Hash documentation and I always think that it's odd.


I'm sure I've done something like this many times:


a = [:a, :b, :c, :d]
 
 ...
 
 if some_condition
     a = [:e, :f]
 end
 


But I never thought to use this instead:


a = [:a, :b, :c, :d]
 
 ...
 
 if some_condition
     a.replace [:e, :f]
 end
 


Which I assume is the intended use. Does this really save memory, or have some other benefit, or is it just a style thing?



Answer:


a = [:e, :f] and a.replace [:e, :f],
the two statements generated instructions as follows:
1.
a = [:a, :b, :c, :d] 
a = [:e, :f] 
instructions:
ruby --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]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]) 
instructions:
ruby --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]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 
The replace method is not faster than assignment operator, but replace can modify receiver array in-place, and, replace method really save memory, this can be seen from rb_ary_replace's source.
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; } 

1 comment: