You don’t have to accept slow Ruby or Rails performance. In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code—but that’s just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you through perils of memory and CPU optimization, profiling, measuring, performance testing, garbage collection, and tuning. You’ll find that all those “hard” things aren’t so difficult after all, and your code will run orders of magnitude faster.
Ruby Performance Optimization: Why Ruby Is Slow, and How to Fix It
by Alexander Dymo
Ruby Performance Optimization
Why Ruby Is Slow, and How to Fix It
by Alexander Dymo
This book is a must-have for anyone bringing performance-sensitive Ruby programs to production. It will teach you how to efficiently hunt down and correct the bottlenecks that make users sad and prevent teams from spending time building new functionality.
- Matt Margolis
Director of application development, Getty Images
A fantastic book. Probably the best tech book I have read in the last few years. It brings together information I can’t just find in 30 minutes of web searches. This book has taught me to be a better developer and to start filling a hole in my skill set.
- Charley Stran
Web developer, TheBlaze | Mercury Radio Arts
Ruby Performance Optimization has changed the way I develop. I now think about simple performance tweaks before I commit code. It is a book I keep close for reference when solving tough performance issues.
- Jeff Holland
Senior software engineer, Ackmann & Dickenson
For programmers who aren’t familiar with what is going on “under the covers,” I think this book will open their eyes to new levels of understanding their code.
- Kim Shrier
Principal, Westryn Internet Services, Shrier and Deihl
About this Title
Release: P1.0 (2015-12-01)
This is the first book ever that consolidates all the Ruby performance optimization advice in one place. It’s your comprehensive guide to memory optimization, CPU optimization, garbage collector tuning, profiling, measurements, performance testing, and more.
You’ll go from performance rookie to expert. First, you’ll learn the best practices for writing Ruby code that’s easy not only on the CPU, but also on memory, and that doesn’t trigger the dreaded garbage collector. You’ll find out that garbage collection accounts for 80% of slowdowns, and often takes more than 50% of your program’s execution time. And you’ll discover the bottlenecks in Rails code and learn how selective attribute loading and preloading can mitigate the performance costs of ActiveRecord.
As you advance to Ruby performance expert, you’ll learn how to profile your code, how to make sense out of profiler reports, and how to make optimization decisions based on them. You’ll make sure slow code doesn’t creep back into your Ruby application by writing performance tests, and you’ll learn the right way to benchmark Ruby.
And finally, you’ll dive into the Ruby interpreter internals to really understand why garbage collection makes Ruby so slow, and how you can tune it up.
What You Need
Some version of Ruby. The advice from this book applies to all modern Ruby versions from 1.9 to 2.2. 80% of the material will also be useful for legacy Ruby 1.8 users, and there is 1.8-specific advice as well.
Contents & Extracts
- What Makes Ruby Code Fast excerpt
- What Makes Ruby Code Slow
- Optimize Memory
- Get into the Performance Mind-set
- Fix Common Performance Problems
- Save Memory
- Optimize Your Iterators
- Write Less Ruby
- Make Rails Faster
- Make ActiveRecord Faster
- Make ActionView Faster
- Measure with Ruby-Prof
- Visualize with Ruby-Prof Printers
- Visualize with KCachegrind (QCachegrind)
- Learn to Optimize with the Profiler
- Optimize but Don’t Break
- Pick Low-Hanging Fruit
- Take a Step Back
- Profile Memory
- Detect Excessive Memory Usage
- Profile Memory with Ruby-Prof
- Measure Memory Yourself with GC#stat and GC::Profiler
- Minimize External Factors
- Make Internals Behave Predictably
- Analyze and Compare Measurements Using Statistics
- Test Performance
- Assert Performance
- Report Slowdowns and Optimizations
- Test Rails Application Performance
- Think Outside the Box
- Cycle Long-Running Instances
- Fork to Run Heavy Jobs
- Do Out-of-Band Garbage Collection
- Tune Your Database
- Buy Enough Resources for Production
- Tune Up the Garbage Collector
- Understand How Ruby Uses Memory
- Know What Triggers GC
- Understand Why GC in Ruby 2.1 and 2.2 is So Much Faster
- Tune Up GC Settings
Alexander Dymo is an entrepreneur, YCombinator alum, free software developer, and book author. Alexander has built many applications with Ruby on Rails for his two startups and has spent long hours optimizing them. He regularly presents at Ruby, Rails, and Postgres conferences.