Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimize the rary_insert().

* before
                               user     system      total        real
filled object / string     0.990000   0.000000   0.990000 (  0.981791)
filled object / numeric    0.930000   0.000000   0.930000 (  0.928791)
empty array / string       0.160000   0.010000   0.170000 (  0.095161)
empty array / numeric      0.010000   0.000000   0.010000 (  0.015316)

* after
                               user     system      total        real
filled object / string     0.060000   0.000000   0.060000 (  0.057018)
filled object / numeric    0.050000   0.000000   0.050000 (  0.051273)
empty array / string       0.080000   0.000000   0.080000 (  0.045073)
empty array / numeric      0.020000   0.000000   0.020000 (  0.014007)

{{{
require 'benchmark'

Benchmark.bm(25) do |x|

  ary  = ['obj'] * 10000
  x.report "filled object / string" do
    1000.times do
      ary.unshift("a")
    end
  end

  ary  = ['obj'] * 10000
  x.report "filled object / numeric" do
    1000.times do
      ary.unshift(0)
    end
  end

  ary = Array.new(10000)
  x.report "empty array / string" do
    1000.times do
      ary.unshift("a")
    end
  end

  ary = Array.new(10000)
  x.report "empty array / numeric" do
    1000.times do
      ary.unshift(0)
    end
  end

end
}}}
  • Loading branch information...
commit a62158335e63440b9aea2efa5dab7133c1a769ae 1 parent 94a97d3
@Watson1978 Watson1978 authored
Showing with 3 additions and 3 deletions.
  1. +3 −3 array.c
View
6 array.c
@@ -390,9 +390,9 @@ rary_insert(VALUE ary, long idx, VALUE val)
}
else if (idx < RARY(ary)->len) {
rary_reserve(ary, RARY(ary)->len + 1);
- for (size_t i = RARY(ary)->len; i > idx; i--) {
- rary_elt_set(ary, i, rary_elt(ary, i - 1));
- }
+ GC_MEMMOVE(&RARY(ary)->elements[RARY(ary)->beg + idx + 1],
+ &RARY(ary)->elements[RARY(ary)->beg + idx],
+ sizeof(VALUE) * (RARY(ary)->len - idx));
rary_elt_set(ary, idx, val);
RARY(ary)->len++;
}
Please sign in to comment.
Something went wrong with that request. Please try again.