Skip to content

Instantly share code, notes, and snippets.

@jespr
Created January 14, 2026 21:49
Show Gist options
  • Select an option

  • Save jespr/c2db5398771ccc028d48fb76f7cc6af1 to your computer and use it in GitHub Desktop.

Select an option

Save jespr/c2db5398771ccc028d48fb76f7cc6af1 to your computer and use it in GitHub Desktop.
analyzing rails performance skill
name description
analyzing-rails-performance
Analyze Ruby on Rails applications for performance issues. Use this skill when asked to find performance problems, optimize Rails apps, check for N+1 queries, investigate slow endpoints, or understand why something is slow. Covers N+1 query detection, missing indexes, inefficient ActiveRecord patterns, and memory bloat.

Rails Performance Analysis

Analyze Rails applications for common performance issues using pattern matching, code analysis, and Rails-specific heuristics.

Analysis Areas

1. N+1 Query Detection

Search for patterns that commonly cause N+1 queries:

# Find iterations over associations without eager loading
ast-grep --lang ruby -p '$COLLECTION.each { |$VAR| $VAR.$ASSOC }'

# Find .map calls accessing associations
ast-grep --lang ruby -p '$COLLECTION.map { |$VAR| $VAR.$ASSOC }'

Red flags to identify:

  • Loops iterating over records and accessing associations
  • Views rendering partials with collection.each that access related records
  • Serializers/presenters accessing associations without preloading

See ./n-plus-one-patterns.md for comprehensive detection patterns.

2. Query Optimization

Missing indexes: Check for columns used in where, find_by, order without indexes:

# Find where clauses to cross-reference with schema
ast-grep --lang ruby -p 'where($COLUMN: $$$)'
ast-grep --lang ruby -p 'find_by($COLUMN: $$$)'
ast-grep --lang ruby -p 'order($COLUMN)'

Inefficient patterns:

  • Model.all followed by Ruby filtering instead of SQL
  • Multiple queries that could be combined with joins
  • pluck on large result sets without limits

See ./query-optimization.md for index analysis and query improvements.

3. Memory & Object Allocation

Common memory issues:

  • Loading entire tables with Model.all instead of batching
  • String concatenation in loops (use arrays + join)
  • Unnecessary object creation in hot paths
# Find potential memory hogs
ast-grep --lang ruby -p 'Model.all'
ast-grep --lang ruby -p '$$.each { |$VAR| $STR + $$$}'

See ./memory-patterns.md for memory optimization guidance.

Analysis Workflow

  1. Identify the scope - specific controller, model, or entire app?
  2. Check for N+1 patterns - most common Rails performance issue
  3. Review database queries - missing indexes, inefficient scopes
  4. Examine memory patterns - especially in background jobs and batch operations
  5. Check eager loading - verify includes, preload, eager_load usage

Key Files to Examine

  • app/controllers/ - N+1 in controller actions
  • app/models/ - scope definitions, callbacks, associations
  • app/views/ - partials iterating over collections
  • app/serializers/ - association access patterns
  • db/schema.rb - existing indexes
  • config/database.yml - connection pool settings

Quick Commands

# Find all includes/preload/eager_load usage
rg 'includes\(|preload\(|eager_load\(' app/

# Find potential N+1 in views
rg '\.each.*\.' app/views/

# Check for missing counter_cache opportunities
ast-grep --lang ruby -p '$$.count'

Reporting

When reporting findings, include:

  • Location: File path and line number
  • Issue: What the performance problem is
  • Impact: Why it matters (query count, memory, response time)
  • Fix: Specific code change to resolve it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment