Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prints io directly instead of converting to string #1084

Merged
merged 1 commit into from
Apr 4, 2020

Conversation

paulcsmith
Copy link
Member

@paulcsmith paulcsmith commented Apr 4, 2020

Closes #507

This prints the io directly to the response, rather than making it a
string first. The bigger the response, the more helpful this will be.

Benchmarks of printing directly v. converting to String first:

require "benchmark"

text = <<-TEXT
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
TEXT

string_text = text * 40_000
io_text = IO::Memory.new(string_text)

Benchmark.ips do |x|
  x.report("string") do
    io = IO::Memory.new
    io.print(io_text.to_s)
  end
  x.report("io") do
    io = IO::Memory.new
    io.print(io_text)
  end
end

Results:

string 942.67  (  1.06ms) (± 1.33%)  12.7MB/op   2.78× slower
    io   2.62k (381.73µs) (± 3.52%)   8.0MB/op        fastest

So far medium/large HTML responses this will shave off about .6ms. Not massive, but a quick and easy win

Closes #507

This prints the io directly to the response, rather than making it a
string first. The bigger the response, the more helpful this will be.
@paulcsmith
Copy link
Member Author

Also, we could further optimize this, but I wanted to get the easy win first. Maybe we can skip making a new IO to speed it up even more. But we'd have to modify TextResponse and do a few other things so I'm gonna put that off for now

@paulcsmith paulcsmith merged commit f7472d4 into master Apr 4, 2020
@paulcsmith paulcsmith deleted the pcs/render-io-directly branch April 4, 2020 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Render directly to IO
1 participant