R
R
Roman Mirilaczvili2020-08-15 21:13:19
ruby
Roman Mirilaczvili, 2020-08-15 21:13:19

What is the best way to process stdin in Ruby?

require "memory_profiler"
report = MemoryProfiler.report do
    $stdout.sync = true
    ARGF.each_char do |c|
        $stdout.print(c)
    end
end

report.pretty_print(to_file: 'memory_profiler.log')


Tested in this way:
dd if=/dev/zero bs=1M count=1 | ruby test_stream.rb | wc -c

With a size of 1MB, the profiler shows consumption as much as 42MB!
ARGF.each_char

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2020-10-16
@2ord

If you read through a once allocated buffer, then everything flies and the memory does not eat ..

require "memory_profiler"

report = MemoryProfiler.report do
  $stdout.sync = true

  buffer_size = 5_000
  buffer = String.new("", capacity: buffer_size)

  while (readed = $stdin.read(buffer_size, buffer))
    $stdout << readed
  end
end

report.pretty_print(to_file: "memory_profiler.log")


Total allocated: 5545 bytes (4 objects)
Total retained: 5041 bytes (1 objects)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question