Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
First of all, thank you for maintaining a great and very useful library.
I've integrated MimeMagic in the Shrine library for handling file uploads, and wanted to do a couple of performance improvements. I want to say upfront, this isn't only for reducing string allocations, it will actually have a significant impact in my context. In Shrine, in addition to passing local files, you can pass remote files represented as IOs, which downloads only how much is needed for
#read
to return the requested bytes.Before sending a PR to update MimeMagic to support this IO-like object (which don't respond to
#seek
), I wanted to first make performance improvements ensuring everything runs the fastest. This PR consists of three distinct changes:StringIO
.IO#read
accepts a "buffer" string as a second argument, which will make that instead of#read
returning a newly allocated string, it will always modify the "buffer" string, thus not making any new allocations. I tested the on a JPEG file, and it saved 400+ string allocations, which were 44KB in total.It would be great if you could review this, so that we can get it merged, and I can proceed with the second PR which avoids reading the whole content (in my case this means downloading the whole file) for IOs which don't respond to
#seek
.