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
Reduce allocations by using #each_with_object #7758
Conversation
5d97640
to
137030c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functional programming can be fun, but isn’t always efficient in this wild world of Ruby we’re living in. 😄 Thanks for fixing this up!
@parkr Sorry, I found one more instance of |
The gains aren't much, but probably non-trivial for much larger sites: Profiler Summary--- master branch https://travis-ci.org/jekyll/jekyll/jobs/561428064
+++ PR branch https://travis-ci.org/jekyll/jekyll/jobs/562630268
- Total allocated: 479.11 MB (4483645 objects)
- Total retained: 18.54 MB (96252 objects)
+ Total allocated: 479.06 MB (4482911 objects)
+ Total retained: 18.54 MB (96251 objects) |
@jekyllbot: merge +dev |
Summary
The loop within
config.reduce({}) { |hsh, (k, v)| hsh.merge(k.to_s => v) }
is going to run for eachkey-value pair in
config
and on each iteration,hsh.merge
is going to allocate a new Hash object.Therefore, if a config file has 20 top-level key-value pairs, this method would've allocated at least 20 Hash objects (of increasing size)..
This can be bad for sites that have numerous top-level config keys (e.g. forks of
minimal-mistakes
theme repo)