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

Switch to mini_mime gem from mime-types #1884

Merged
merged 1 commit into from
Jun 28, 2017

Conversation

Fryguy
Copy link
Contributor

@Fryguy Fryguy commented Jun 28, 2017

mini_mime is a much more performant replacement for the mime-types gem. It uses the same database as mime-types, but loads it in a much more efficient manner, improving the number of allocations, memory, and speed. The mail gem, which is a core component of Rails, has already switched to it. See mikel/mail#1059 for more details on the mail gem changes, as well as the some performance numbers. Bonus is that mini_mime supports Ruby versions all the way back to 1.8.7, so there's no need for a caveat anymore.

cc @SamSaffron

@Fryguy
Copy link
Contributor Author

Fryguy commented Jun 28, 2017

Performance details (quoting @SamSaffron from the referenced PR)

The mime types gem (even with the magical columnar store by @jeremyevans) is a memory hog.

In particular on boot, mime/types/columnar will allocate 109k objects and end up with a retained count of 31K objects.

This bloat leaves objects that need to be marked and swept every major GC and bloats ruby processes.

To circumvent mini_mime was created.

It uses the exact same database as the full fledged mime types and is capable of only loading stuff on demand with a practical, safe and bound in-memory cache.

mini_mime will allocate 398 objects on boot and only retain 62 (due to rubygems inefficiency)

In table form:

boot allocations boot retained lookup lookup uncached
mini_mime 398 62 641K/s 33K/s
mime-types 109796 31165 361K/s 361K/s

The performance of mini_mime is pretty good, cached lookups are faster than the mime types gem and uncached lookups are 10x slower. (which is really not a huge issue considering the memory savings)

@twalpole
Copy link
Member

twalpole commented Jun 28, 2017

LGTM, and no need for fixing the broken sorting from mime-types either apparently - thanks!

@twalpole twalpole merged commit bd5ec51 into teamcapybara:master Jun 28, 2017
@Fryguy Fryguy deleted the switch_to_mini_mime branch June 28, 2017 20:49
carlosantoniodasilva added a commit to carlosantoniodasilva/httparty that referenced this pull request Nov 10, 2022
`mini_mime` is a minimal mime type library that's more performant and
less memory hungry.
https://github.com/discourse/mini_mime

It has replaced `mime-types` in the `mail` gem: (which is a dependency
of `actionmailer`, and by extension, `rails`)
mikel/mail#1059

As well as `capybara`:
teamcapybara/capybara#1884

Which also means using it as a dependency of `httparty` would be able to
reuse the same dependency that should be already available in most Rails
apps, instead of pulling in an extra `mime-types` dependency.

The change in code is pretty straightforward, the same one made by the
capybara PR linked above.
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.

2 participants