SlideShare a Scribd company logo
Ruby/Rails Performance Tips
Patrick McSweeny
About Me
 I really like Ruby
 Organizer of Ann Arbor Ruby Brigade
 Looking for speakers
Notes on format
 Briefly touch on many different topics
 Self-study encouraged
When to Optimize for Performance
 Application is running slowly
 High resource consumption
 Hosting is getting expensive
 Need to scale
When Not to Optimize
 Still a beginner
 Early stages of application development
 Limited number of users for app
Background on Ruby
 High level language
 Memory allocations handled by interpreter
 Garbage collector frees up unused memory
 Crucial for limiting memory consumption
 Resource intensive
Use the latest version of Ruby possible
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
How to Speed Up Ruby
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!
Dive into the Enumerable Module
 Use map or map! to run the same method on all
items in collection

Dive into the Enumerable Module
 Use .reject/delete_if? to delete items from
collection
 Use .any? instead of .count > 0
Use a Lightweight Stack
 Sinatra - Ruby DSL for web framework
 Cuba – Super-lightweight web framework in
pure Ruby
 Mote – Lightweight templating engine
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
Write Less Ruby
 Create gems that use low level language
 C/C++ - not for beginners
 Rust – low-level programming done safely

Take the logic
and put it in the database
Fast Scalable
Ruby
Databases
✓ ✓
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
Alternative Implementations of Ruby
 JRuby – good for heavy loads
 Rubinius – modern approach to implementation
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

Caching
 Page caching – not very flexible
 Action caching – runs filters such as
authentication before loading page
 Fragment Caching
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

Russian Doll Caching
Speeding Up Rails
 Limit use of loops in ActiveRecord
 Pass array of hashes to create method
 Use update_all
 destroy_all/delete_all
Speeding Up Rails
 Render a collection of objects in partials all at
once
 ex:
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]
@PatrickMcSweeny

More Related Content

What's hot

Brief introduction to Distributed Deep Learning
Brief introduction to Distributed Deep LearningBrief introduction to Distributed Deep Learning
Brief introduction to Distributed Deep Learning
Adam Gibson
 
Aerospike - fast and furious caching @ Burgasconf 2016
Aerospike - fast and furious caching @ Burgasconf 2016Aerospike - fast and furious caching @ Burgasconf 2016
Aerospike - fast and furious caching @ Burgasconf 2016
Tihomir Trifonov
 
Drupal Performance
Drupal PerformanceDrupal Performance
Drupal Performance
Kornel Lugosi
 
From rest api to graph ql a 10 year journey
From rest api to graph ql a 10 year journeyFrom rest api to graph ql a 10 year journey
From rest api to graph ql a 10 year journey
Arno Schulz
 
Apache pulsar
Apache pulsarApache pulsar
Apache pulsar
Kotireddy Sareddy
 
Web Performance Part 3 "Server-side tips"
Web Performance Part 3  "Server-side tips"Web Performance Part 3  "Server-side tips"
Web Performance Part 3 "Server-side tips"
Binary Studio
 
Repository 3.7
Repository 3.7Repository 3.7
Repository 3.7
StephenKardian
 
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
ScyllaDB
 
Workers and Worker Patterns at Scale
Workers and Worker Patterns at ScaleWorkers and Worker Patterns at Scale
Workers and Worker Patterns at Scale
Chad Arimura
 
MySQL-Mixer-Livenation
MySQL-Mixer-LivenationMySQL-Mixer-Livenation
MySQL-Mixer-Livenation
Hari Tatrakal
 
Scrappy
ScrappyScrappy
Scrappy
Vishwas N
 
Sergei Nesterov "How to Speed Up the Build of Large Projects. Overview and C...
Sergei Nesterov  "How to Speed Up the Build of Large Projects. Overview and C...Sergei Nesterov  "How to Speed Up the Build of Large Projects. Overview and C...
Sergei Nesterov "How to Speed Up the Build of Large Projects. Overview and C...
LogeekNightUkraine
 
Machine Learning for (JVM) Developers
Machine Learning for (JVM) DevelopersMachine Learning for (JVM) Developers
Machine Learning for (JVM) Developers
Mateusz Dymczyk
 

What's hot (13)

Brief introduction to Distributed Deep Learning
Brief introduction to Distributed Deep LearningBrief introduction to Distributed Deep Learning
Brief introduction to Distributed Deep Learning
 
Aerospike - fast and furious caching @ Burgasconf 2016
Aerospike - fast and furious caching @ Burgasconf 2016Aerospike - fast and furious caching @ Burgasconf 2016
Aerospike - fast and furious caching @ Burgasconf 2016
 
Drupal Performance
Drupal PerformanceDrupal Performance
Drupal Performance
 
From rest api to graph ql a 10 year journey
From rest api to graph ql a 10 year journeyFrom rest api to graph ql a 10 year journey
From rest api to graph ql a 10 year journey
 
Apache pulsar
Apache pulsarApache pulsar
Apache pulsar
 
Web Performance Part 3 "Server-side tips"
Web Performance Part 3  "Server-side tips"Web Performance Part 3  "Server-side tips"
Web Performance Part 3 "Server-side tips"
 
Repository 3.7
Repository 3.7Repository 3.7
Repository 3.7
 
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
Scylla Summit 2018: Rebuilding the Ceph Distributed Storage Solution with Sea...
 
Workers and Worker Patterns at Scale
Workers and Worker Patterns at ScaleWorkers and Worker Patterns at Scale
Workers and Worker Patterns at Scale
 
MySQL-Mixer-Livenation
MySQL-Mixer-LivenationMySQL-Mixer-Livenation
MySQL-Mixer-Livenation
 
Scrappy
ScrappyScrappy
Scrappy
 
Sergei Nesterov "How to Speed Up the Build of Large Projects. Overview and C...
Sergei Nesterov  "How to Speed Up the Build of Large Projects. Overview and C...Sergei Nesterov  "How to Speed Up the Build of Large Projects. Overview and C...
Sergei Nesterov "How to Speed Up the Build of Large Projects. Overview and C...
 
Machine Learning for (JVM) Developers
Machine Learning for (JVM) DevelopersMachine Learning for (JVM) Developers
Machine Learning for (JVM) Developers
 

Similar to Ruby/Rails Performance Tips

Rails Concept
Rails ConceptRails Concept
Rails Concept
Javed Hussain
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
guest4faf46
 
Workin ontherailsroad
Workin ontherailsroadWorkin ontherailsroad
Workin ontherailsroad
Jim Jones
 
WorkinOnTheRailsRoad
WorkinOnTheRailsRoadWorkinOnTheRailsRoad
WorkinOnTheRailsRoad
webuploader
 
Front Range PHP NoSQL Databases
Front Range PHP NoSQL DatabasesFront Range PHP NoSQL Databases
Front Range PHP NoSQL Databases
Jon Meredith
 
MongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDBMongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDB
Rick Copeland
 
Drupal Backend Performance and Scalability
Drupal Backend Performance and ScalabilityDrupal Backend Performance and Scalability
Drupal Backend Performance and Scalability
Ashok Modi
 
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Udaya Kiran
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
TAInteractive
 
Ruby on Rails
Ruby on Rails Ruby on Rails
Ruby on Rails
thinkahead.net
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
TAInteractive
 
Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex
Espen Brækken
 
DrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performanceDrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performance
Ashok Modi
 
Practical JRuby
Practical JRubyPractical JRuby
Practical JRuby
David Keener
 
DiUS Computing Lca Rails Final
DiUS  Computing Lca Rails FinalDiUS  Computing Lca Rails Final
DiUS Computing Lca Rails Final
Robert Postill
 
Ruby On Rails - Rochester K Linux User Group
Ruby On Rails - Rochester K Linux User GroupRuby On Rails - Rochester K Linux User Group
Ruby On Rails - Rochester K Linux User Group
Jose de Leon
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
sunniboy
 
Ruby and Security
Ruby and SecurityRuby and Security
Ruby and Security
Carl Sampson, CSSLP
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
chamomilla
 
Ruby on rails for beginers
Ruby on rails for beginersRuby on rails for beginers
Ruby on rails for beginers
shanmukhareddy dasi
 

Similar to Ruby/Rails Performance Tips (20)

Rails Concept
Rails ConceptRails Concept
Rails Concept
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Workin ontherailsroad
Workin ontherailsroadWorkin ontherailsroad
Workin ontherailsroad
 
WorkinOnTheRailsRoad
WorkinOnTheRailsRoadWorkinOnTheRailsRoad
WorkinOnTheRailsRoad
 
Front Range PHP NoSQL Databases
Front Range PHP NoSQL DatabasesFront Range PHP NoSQL Databases
Front Range PHP NoSQL Databases
 
MongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDBMongoATL: How Sourceforge is Using MongoDB
MongoATL: How Sourceforge is Using MongoDB
 
Drupal Backend Performance and Scalability
Drupal Backend Performance and ScalabilityDrupal Backend Performance and Scalability
Drupal Backend Performance and Scalability
 
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Ruby on Rails
Ruby on Rails Ruby on Rails
Ruby on Rails
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex Ruby on Rails (RoR) as a back-end processor for Apex
Ruby on Rails (RoR) as a back-end processor for Apex
 
DrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performanceDrupalCampLA 2011: Drupal backend-performance
DrupalCampLA 2011: Drupal backend-performance
 
Practical JRuby
Practical JRubyPractical JRuby
Practical JRuby
 
DiUS Computing Lca Rails Final
DiUS  Computing Lca Rails FinalDiUS  Computing Lca Rails Final
DiUS Computing Lca Rails Final
 
Ruby On Rails - Rochester K Linux User Group
Ruby On Rails - Rochester K Linux User GroupRuby On Rails - Rochester K Linux User Group
Ruby On Rails - Rochester K Linux User Group
 
Ruby On Rails Tutorial
Ruby On Rails TutorialRuby On Rails Tutorial
Ruby On Rails Tutorial
 
Ruby and Security
Ruby and SecurityRuby and Security
Ruby and Security
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Ruby on rails for beginers
Ruby on rails for beginersRuby on rails for beginers
Ruby on rails for beginers
 

Recently uploaded

Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 

Recently uploaded (20)

Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 

Ruby/Rails Performance Tips

  • 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
  • 7. Use the latest version of Ruby possible
  • 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
  • 9. How to Speed Up Ruby
  • 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 
  • 16. Take the logic and put it in the database
  • 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
  • 19. Alternative Implementations of Ruby  JRuby – good for heavy loads  Rubinius – modern approach to implementation
  • 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]