Lazy load dependencies to reduce memory on startup #47
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.
I noticed that
hair_trigger
was requiring a lot of memory on startup. Usingderailed_benchmarks
I tracked that down to the loading of the
ruby_parser
andruby2ruby
gems.Since those libraries are only needed by the
HairTrigger::MigrationReader
itis possible to use
autoload
to lazy-load those libraries only when needed.In a sample case of a Gemfile that only includes
activerecord
andhairtrigger
the total memory allocated just to load the Gemfile dropped from 53MB to 23MB. At least 6MB of that memory is not able to be garbage collected. The observed process memory dropped from ~25MB to ~16MB.
Here is a process for recreating those performance results...
Create a
Gemfile.derailed
file in the project root directoryBundle install the gemfile and execute the bundle:objects and bundle:mem tasks to see the measurements.
This can be run on master and on the branch for comparison.
Before (master): bundle:objects
After (pull request): bundle:objects
Before (master): 3 runs of bundle:mem - the results vary
After (pull request): 3 runs of bundle:mem