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

Performance drop of MiniMagick::Image.open vs. MiniMagick::Image.new #2062

Closed
tudorpavel opened this issue Nov 3, 2016 · 0 comments · Fixed by #2298
Closed

Performance drop of MiniMagick::Image.open vs. MiniMagick::Image.new #2062

tudorpavel opened this issue Nov 3, 2016 · 0 comments · Fixed by #2298

Comments

@tudorpavel
Copy link

tudorpavel commented Nov 3, 2016

My use case is that I want to create image previews from the first page of multi-page PDF document uploads.

I've used @davidchristiandy's solution from here.

version :cover do
  process :convert_cover => :png
end

def convert_cover(format)
  manipulate! do |img| # this is ::MiniMagick::Image instance
    img.format(format.to_s.downcase, 0)
    img
  end
end

The problem I noticed is a significant performance drop for large PDFs like books with tens/hundreds of pages which didn't make sense to me. Uploading one such book took about 9 seconds to process. So, I began tracing the problem.

First, I converted the first page directly using imagemagick and it ran in less than 1 second.

$ convert book.pdf[0] test-imagemagick.png

Then, I used MiniMagick directly from a rails console and it also ran in less than 1 second.

img = MiniMagick::Image.new('book.pdf') # takes about 1 second
img.format('png', 0)

Looking through the implementation i found that manipulate! uses MiniMagick::Image.open.

When I ran that in rails console, I had found the performance drop.

img = MiniMagick::Image.open('book.pdf') # takes about 9 seconds

Turns out, the MiniMagick::Image.open method makes a copy of the loaded file instead of manipulating it directly and that's most likely the reason for the performance drop.

My question is, couldn't manipulate! just use MiniMagick::Image.new since the original file is being overridden anyway?

Also, having different versions in the Uploader already makes copies with prefixes such as cover_ before running manipulate!, doesn't it?

There might be other use cases that would be affected by this change, but I'm not sure. If I have more time, I can investigate the implications further.

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 a pull request may close this issue.

1 participant