

Speeding up Rails 4.2 - mdasen
http://samsaffron.com/archive/2014/12/01/speeding-up-rails-4-2

======
matthewmacleod
This is really great – but I guess I'm a little concerned that large
performance regressions aren't being caught at an earlier stage. I wonder if
there's scope to automatically keep tabs on Rails Edge performance?

~~~
jpgvm
It would be great if there was a performance benchmark run on CI stack or
something and writing output in parseable format so it could be graphed over
time etc.

~~~
gavingmiller
Sam has also blogged about this for Ruby specifically:
[http://samsaffron.com/archive/2013/12/11/call-to-action-
long...](http://samsaffron.com/archive/2013/12/11/call-to-action-long-running-
ruby-benchmark)

------
akanet
This is exceedingly great work. This is the stuff I love coming to HN for. I
had no idea Discourse had a dual-boot mode, much less that it had become a de-
facto macro benchmark for both Ruby and Rails. That is awesome.

------
dynjo
You guys might like our latest gem, it outputs ruby-prof as FlameGraphs
[https://github.com/oozou/ruby-prof-flamegraph](https://github.com/oozou/ruby-
prof-flamegraph)

~~~
jpgvm
Awesome, I love flamegraphs for finding performance bottlenecks.

------
solutionyogi
Sam is a smart guy and an awesome developer. He contributed tons of code for
.NET community when he worked at Stack Overflow.

I have done a fair amount of performance optimization work in .NET/C#, so I
was curious as to what kind of optimizations are involved in Ruby.

One of the optimization was this:

[https://github.com/SamSaffron/rails/commit/9aa2b2d7bf52c0f39...](https://github.com/SamSaffron/rails/commit/9aa2b2d7bf52c0f3974090cd033341be2edeae6d)

Three lessons:

1\. Always use profiler to identify code bottlenecks. I don't think I would
have ever guessed that the string literal is costing memory allocations
causing performance issues.

2\. I find it sad that this kind of optimization is required in 2015. In any
statically typed language, this would be completely unnecessary.

3\. Performance optimization makes code less readable. I think it would make
sense to add comment explaining why constant ID was declared.

Here's another:

[https://github.com/SamSaffron/rails/commit/a668b09ee387bf241...](https://github.com/SamSaffron/rails/commit/a668b09ee387bf241a3481a764a18a5c8a4df8cd)

Same story.

~~~
m0th87
String interning has nothing to do with static typing. AFAIK Ruby supports it
with symbols and Python with strings in general.

~~~
riffraff
Recent rubys also supports it for frozen strings (which is the reason it's a
frozen string in the commit, probably).

The reason it's not done by default is that with mutable strings it would be
more complicates/would require a different string implementation, I guess.

------
grk
Ruby 2.2 will add almost-automatic-freezing of string hash keys[1]. I wonder
how big of an impact this will have on Rails performance.

[1] [http://www.sitepoint.com/unraveling-string-key-
performance-r...](http://www.sitepoint.com/unraveling-string-key-performance-
ruby-2-2/)

------
mikecmpbll
Great write-up, and thanks Sam and Sean!

Makes me wonder how bad it would have been without Adequate Record? I would
imagine it's a bit disheartening for Aaron that all his hard work making AR
2-times faster have been obliterated by other stuff! ..

~~~
jrochkind1
Makes me wonder if Adequate Record has much impact at all on typical app
patterns. Has AdRec been obliterated by other stuff, or is it a microbenchmark
that just didn't effect the OP's app much?

------
VeejayRampay
Sam Saffron has great write-ups. His article on Ruby 2.1 and garbage
collection fine-tuning [1] is a must-read

[1] [http://samsaffron.com/archive/2014/04/08/ruby-2-1-garbage-
co...](http://samsaffron.com/archive/2014/04/08/ruby-2-1-garbage-collection-
ready-for-production)

------
patio11
Thanks, this is both enormously helpful work for those in the Rails community
who depend on the new hotness, _and_ a great writeup for increasing
engineering skill levels throughout the community.

------
stanislavb
Amazing work! I've bookmarked this post. I think that this approach of
profiling Discourse on Rails could be use for any other app/service that is to
be migrated to Rails 4.2.

------
Yadi
You are the man! Thanks for the great insight on the performance.

