Rails 4.2.0 beta1 was released August 20, 2014. And according to dhh’s release post, and I quote,
a lot of common queries are now no less than twice as fast in Rails 4.2!
So, what did Rails team – or more specifically – tenderlove (Aaron Patterson) do to improve Rails/ActiveRecord so much? Let’s find out through some commits.
Here are some tools Aaron has used for measuring performance according to his Cascadia Ruby 2014 talk:
You should definitely checkout these tools. It would be very useful in your daily Ruby/Rails development.
GitHub Commit: drastically reduce object allocations
Inside the tag_option method, the
value variable was html escaped first, then interpolated into a string.
1 2 3 4 5
And if digging into html_escape (alias as h) method, we will see that there will be a AS::SafeBuffer object allocated:
1 2 3 4 5 6 7 8 9
tag_option, there will always be an AS::SafeBuffer useless object allocated. This could be solved by adding another escape method but doesn’t wrap string with an AS::SafeBuffer. And
tag_option should call that method instead of the old one.
And this tiny change reduced the AS::SafeBuffer objects from 1527 per request to about 500 per request according to Aaron’s benchmark. It is trully drastically, awesome!
GitHub Commit: No need for another hash allocation / merge!
This commit is very simple, but it should really attrack our attention when writing Ruby codes.
Hash#merge! will allocate a new hash, but with
Hash# this would not happen. And accoriding to my benchmark(I wrote a blog about Performance Differences in Ruby, you may would like to check it out) , it really matters.
1 2 3 4 5 6 7
There are a large number of commits like this in Rails repo recently (because performance really matters, right?).
This commit by @sferik, changing
Hash#keys.eachwill allocate an array of keys, but
Hash#each_keyiterates through the keys without allocating a new array. I also benchmark on this in my article I mentioned above.
And this commit by Aaron is also the same, use
Hash#each_keyto avoid some objects allocation.
Or this one.
@sferik gave a talk about these skills at Baruco 2014, and he is also who made me want to blog and benchmark these in my article. The video has not been released, but you should definitely check out his slides Writing Fast Ruby.
GitHub Commit: reduce object allocations
This commit is basically same with the previous one. It’s about performance differences on how to use
Hash#zip. And Aaron’s commit message explains it all.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
When somenone outsite of Ruby/Rails community talks bout Rails, the performance will always be brought to the conversation. And it really concerns developers when choosing tools to build their apps. After giving so much attention to impove it, we could say Rails is getting faster and Rails will be much more faster later.
So thanks to everyone who has contributed to Rails performance improvement, you guys make this community better and better.