Code Review: Files3

tfpt review “/shelveset:Files3;REDMOND\tomat”

Improves performance for file IO#read method.
Previously we were 10x slower than MRI, now we are approx. 1.5-times
faster.

Tomas

MRI:
D:\temp>ruby file.rb
user system total real
write big.txt
6.287000 0.046000 6.333000 ( 6.337000)
write big_lines.txt
0.265000 0.063000 0.328000 ( 0.327000)
read big.txt
100 x 1 x 10000000 3.635000 1.825000 5.460000 ( 5.463000)
100 x 10 x 1000000 3.370000 2.122000 5.492000 ( 5.493000)
100 x 1000 x 10000 3.900000 1.747000 5.647000 ( 5.650000)
100 x 10000 x 1000 5.335000 1.326000 6.661000 ( 6.680000)
10 x 100000 x 100 1.404000 0.218000 1.622000 ( 1.623000)
10 x 1000000 x 10 10.858000 0.203000 11.061000 ( 11.065000)
1 x 10000000 x 1 10.546000 0.000000 10.546000 ( 10.565000)
read big_lines.txt
100 x 1 x 10000000 3.603000 1.888000 5.491000 ( 5.494000)
100 x 10 x 1000000 3.479000 2.043000 5.522000 ( 5.525000)
100 x 1000 x 10000 3.978000 1.716000 5.694000 ( 5.696000)
100 x 10000 x 1000 5.179000 1.529000 6.708000 ( 6.711000)
10 x 100000 x 100 1.451000 0.156000 1.607000 ( 1.607000)
10 x 1000000 x 10 10.842000 0.203000 11.045000 ( 11.050000)
1 x 10000000 x 1 10.530000 0.031000 10.561000 ( 10.581000)

IronRuby:
D:\temp>rbr file.rb
user system total real
write big.txt
4.726830 0.046800 4.773631 ( 4.728769)
write big_lines.txt
1.060807 0.046800 1.107607 ( 1.045636)
read big.txt
100 x 1 x 10000000 1.466409 1.216808 2.683217 ( 2.677968)
100 x 10 x 1000000 2.168414 0.982806 3.151220 ( 3.150579)
100 x 1000 x 10000 1.653611 0.826805 2.480416 ( 2.481799)
100 x 10000 x 1000 2.433616 1.669211 4.102826 ( 4.105114)
10 x 100000 x 100 0.811205 0.156001 0.967206 ( 0.967746)
10 x 1000000 x 10 6.567642 0.124801 6.692443 ( 6.696175)
1 x 10000000 x 1 6.255640 0.031200 6.286840 ( 6.290346)
read big_lines.txt
100 x 1 x 10000000 1.762811 1.216808 2.979619 ( 2.981281)
100 x 10 x 1000000 2.620817 0.780005 3.400822 ( 3.402718)
100 x 1000 x 10000 1.840812 1.216808 3.057620 ( 3.059325)
100 x 10000 x 1000 2.605217 1.950013 4.555229 ( 4.557770)
10 x 100000 x 100 0.826805 0.171601 0.998406 ( 0.998963)
10 x 1000000 x 10 6.614442 0.312002 6.926444 ( 6.930307)
1 x 10000000 x 1 6.333641 0.124801 6.458441 ( 6.493261)

require ‘benchmark’

File.delete(“big.txt”) rescue 0
File.delete(“big_lines.txt”) rescue 0

Benchmark.bm do |x|
puts “write big.txt”
x.report {
File.open(“big.txt”, “wb”) { |f|
10_000_000.times { f.write(‘x’) }
}
}

puts “write big_lines.txt”
x.report {
File.open(“big_lines.txt”, “wb”) { |f|
2.times {
1_000.times { f.write(‘x’ * 1000 + “\r\n”) }
1_000.times { f.write(‘x’ * 1000 + “\r”) }
1_000.times { f.write(‘x’ * 1000 + “\r\n”) }
1_000.times { f.write(‘x’ * 1000 + “\n”) }
1_000.times { f.write(‘x’ * 1000 + “\r\n”) }
}
}
}

[“big.txt”, “big_lines.txt”].each do |file|
puts “read #{file}”

     [
          [100,          1, 10_000_000],
          [100,         10,  1_000_000],
          [100,      1_000,     10_000],
          [100,     10_000,      1_000],
          [ 10,    100_000,        100],
          [ 10,  1_000_000,         10],
          [  1, 10_000_000,          1],
     ].each { |n, m, s|
            x.report("#{n} x #{m} x #{s}") {
                 File.open(file, "r") { |f|
                   n.times {
                       f.seek(0);
                       m.times {
                         f.read(s)
                       }
                   }
                 }
            }
     }

end
end

F***** awesome!!! (damn MS curse filter, I need to express my emotions
here!). Thanks Tomas! This will improve Rails perf also =)