SHARE
TWEET

Untitled

a guest Oct 19th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'zlib'
  2. require 'benchmark'
  3. require 'rubygems/package'
  4. require 'rubygems/package/tar_reader'
  5.  
  6. duration = Benchmark.realtime do
  7.   tar = Gem::Package::TarReader.new(Zlib::GzipReader.open(ARGV.first))
  8.   tar.each { |_| }
  9. end
  10.  
  11. p duration
  12.  
  13. class Gem::Package::TarReader
  14.   def each
  15.     return enum_for __method__ unless block_given?
  16.  
  17.     use_seek = @io.respond_to?(:seek)
  18.  
  19.     until @io.eof? do
  20.       header = Gem::Package::TarHeader.from @io
  21.       return if header.empty?
  22.  
  23.       entry = Gem::Package::TarReader::Entry.new header, @io
  24.       size = entry.header.size
  25.  
  26.       yield entry
  27.  
  28.       skip = (512 - (size % 512)) % 512
  29.       pending = size - entry.bytes_read
  30.  
  31.       if use_seek
  32.         begin
  33.           # avoid reading if the @io supports seeking
  34.           @io.seek pending, IO::SEEK_CUR
  35.           pending = 0
  36.         rescue Errno::EINVAL
  37.         end
  38.       end
  39.  
  40.       # if seeking didn't work
  41.       while pending > 0 do
  42.         bytes_read = @io.read([pending, 4096].min).size
  43.         raise UnexpectedEOF if @io.eof?
  44.         pending -= bytes_read
  45.       end
  46.  
  47.       @io.read skip # discard trailing zeros
  48.  
  49.       # make sure nobody can use #read, #getc or #rewind anymore
  50.       entry.close
  51.     end
  52.   end
  53. end
  54.  
  55. duration = Benchmark.realtime do
  56.   tar = Gem::Package::TarReader.new(Zlib::GzipReader.open(ARGV.first))
  57.   tar.each { |_| }
  58. end
  59.  
  60. p duration
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top