This document provides tips for improving the performance of Ruby on Rails applications. It recommends using the latest Ruby version for performance improvements to memory usage and garbage collection. Specific tips include avoiding temporary variables, chaining methods together, using Enumerable methods like map and reject, using lightweight frameworks like Sinatra, writing database logic in the database, eager loading associations, caching at different levels, and optimizing ActiveRecord queries and routes.
2. About Me
I really like Ruby
Organizer of Ann Arbor Ruby Brigade
Looking for speakers
3. Notes on format
Briefly touch on many different topics
Self-study encouraged
4. When to Optimize for Performance
Application is running slowly
High resource consumption
Hosting is getting expensive
Need to scale
5. When Not to Optimize
Still a beginner
Early stages of application development
Limited number of users for app
6. Background on Ruby
High level language
Memory allocations handled by interpreter
Garbage collector frees up unused memory
Crucial for limiting memory consumption
Resource intensive
8. Use Newer Rubies
Significant performance improvements with
each version
Reduced memory consumption
Garbage collection is more efficient
Rails 4.2.8 and up is compatible with Ruby 2.4
10. Avoid using temporary variables
Cuts down on memory consumption
Modify variables in place
ex: str.downcase! instead of new = str.downcase
Chain methods together (i.e. functional
programming)
ex: str.downcase.reverse!
11. Dive into the Enumerable Module
Use map or map! to run the same method on all
items in collection
12. Dive into the Enumerable Module
Use .reject/delete_if? to delete items from
collection
Use .any? instead of .count > 0
13. Use a Lightweight Stack
Sinatra - Ruby DSL for web framework
Cuba – Super-lightweight web framework in
pure Ruby
Mote – Lightweight templating engine
14. Use a Lightweight Stack
Use only what you need from Rails
Use ActiveRecord by itself
Create your own model layer
Use API Controller for returning JSON
Create your own controller
ActionController consists of
Metal – bare-bones controller class
A bunch of included modules
Use Metal Class and only include modules you need
15. Write Less Ruby
Create gems that use low level language
C/C++ - not for beginners
Rust – low-level programming done safely
18. Put the logic in the database
Write custom queries for advanced needs
Joins
Sub-queries
Aggregate (group by) functions
Math
Custom database functions
Database views (named queries)
Downside is logic is in two different places
20. Speeding up Rails
Understand queries executed by ActiveRecord
ex: Use author.book_ids instead of
author.books.pluck(:id)
Use sub-queries, other advanced SQL techniques
Eager load associations
ex: books = Book.includes(:author).all
Cuts down on total number of queries executed
Prevents n + 1 queries
21. Caching
Page caching – not very flexible
Action caching – runs filters such as
authentication before loading page
Fragment Caching
22. Russian Doll Caching
New in Rails 5
Cache fragment inside a cache fragment – and
so on
Child object cache expiration triggers parent
object cache expiration
24. Speeding Up Rails
Limit use of loops in ActiveRecord
Pass array of hashes to create method
Use update_all
destroy_all/delete_all
25. Speeding Up Rails
Render a collection of objects in partials all at
once
ex:
26. Speeding Up Rails
Clean up routes file
Information in file is parsed on every request app
receives
Put most used routes at the top
Limit auto-generated routes with only and except
params
ex: resources :books, only: [:index, :show]