Skip to content
moai-essentials-perf logo

Performance Optimization Expert

moai-essentials-perf

Performance optimization with profiling, memory analysis, and benchmarking

SKILL.md

Full skill instructions

Performance Optimization Expert

Application Profiling & Performance Tuning

Focus: CPU/​Memory Profiling, Benchmarking, Optimization Patterns
Tools: Python (Scalene, cProfile), Node.js (Clinic.js), Go (pprof)


Overview

Systematic approach to identifying and fixing performance bottlenecks.

Core Techniques

  1. Profiling: Measure CPU, memory, I/​O usage.
  2. Benchmarking: Compare performance before/​after changes.
  3. Optimization: Apply targeted improvements (caching, lazy loading, parallelism).

Implementation Patterns

1. Python Profiling (Scalene)

Installation:

pip install scalene

Usage:

# Profile CPU, memory, and GPU
scalene my_script.py

# HTML report
scalene --html --outfile profile.html my_script.py

Code-level profiling:

from scalene import scalene_profiler

scalene_profiler.start()
# Code to profile
result = expensive_computation()
scalene_profiler.stop()

2. Memory Optimization

Common Issues:

  • Large data structures held in memory
  • Circular references preventing GC
  • Inefficient data structures (lists vs generators)

Example (Python):

# Bad: Load entire file into memory
data = [line for line in open('large_file.txt')]

# Good: Use generator
def read_lines(filename):
    with open(filename) as f:
        for line in f:
            yield line.strip()

3. Database Query Optimization

N+1 Query Problem:

# Bad: N+1 queries
users = User.query.all()
for user in users:
    posts = Post.query.filter_by(user_id=user.id).all()  # N queries

# Good: Eager loading
users = User.query.options(joinedload(User.posts)).all()

Indexing:

-- Create index on frequently queried columns
CREATE INDEX idx_user_email ON users(email);

4. Caching Strategies

In-Memory Cache (Python):

from functools import lru_cache

@lru_cache(maxsize=128)
def expensive_function(x):
    # Expensive computation
    return result

Redis Cache:

import redis

cache = redis.Redis(host='localhost', port=6379)

def get_data(key):
    cached = cache.get(key)
    if cached:
        return cached

    data = fetch_from_database(key)
    cache.setex(key, 3600, data)  # Cache for 1 hour
    return data

5. Async I/​O (Node.js)

Avoid blocking I/​O:

// Bad: Synchronous
const data = fs.readFileSync("large_file.txt", "utf8");

// Good: Asynchronous
const data = await fs.promises.readFile("large_file.txt", "utf8");

Best Practices

  1. Measure First: Don't optimize without profiling.
  2. Target Hotspots: Focus on the 20% causing 80% slowdown.
  3. Benchmark: Use realistic workloads, not microbenchmarks.
  4. Monitor Production: Use APM tools (New Relic, Datadog).

Validation Checklist

  • Profiled: Code profiled with appropriate tool?
  • Baseline: Performance baseline established?
  • Optimized: Hotspots identified and fixed?
  • Benchmarked: Improvements measured with benchmarks?
  • Monitored: Production metrics tracked?

Related Skills

  • moai-domain-backend: API optimization
  • moai-domain-monitoring: APM setup
  • moai-devops-docker: Container resource limits

Last Updated: 2025-11-20

More DevOps & CI/CD skills

Project scaffolding, deployment configuration, and CI/CD setup for Google ADK agents.

6K 472.6K
View

Set up tracing, logging, and monitoring for deployed ADK agents across Cloud Trace, BigQuery, and third-party platforms.

6K 472.6K
View

Enterprise Azure infrastructure architect generating Bicep or Terraform from workload descriptions.

1.5K 454.3K
View
azure-kubernetes logo
DevOps & CI/CD

azure-kubernetes

Plan and configure production-ready Azure Kubernetes Service clusters with Day-0 and Day-1 best practices.

1.5K 447.1K
View

Raw mechanical interfaces fusing Swiss typographic print with military terminal aesthetics. Rigid grids, extreme type scale contrast, utilitarian color, analog degradation effects. For data-heavy dashboards, portfolios, or editorial sites that need to feel like declassified blueprints.

92.7K 354.9K
View
just-scrape logo
DevOps & CI/CD

just-scrape

Web search, scraping, extraction, crawling, and monitoring via ScrapeGraph AI CLI.

69 245K
View

Skill for working with Firebase Hosting (Classic). Use this when you want to deploy static web apps, Single Page Apps (SPAs), or simple microservices. Do NOT use for Firebase App Hosting.

462 159.7K
View

Deploy and manage web apps with Firebase App Hosting. Use this skill when deploying Next.js/Angular apps with backends.

462 159.1K
View
deploy-to-vercel logo
DevOps & CI/CD

deploy-to-vercel

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

31.9K 146.6K
View
programmatic-seo logo
DevOps & CI/CD

programmatic-seo

Build SEO-optimized pages at scale using templates, data, and proven playbook patterns.

53.3K 140.1K
View

Design and build isolated, reusable Convex backend components with clear boundaries and app-facing wrappers.

63 123.5K
View

Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g. "deploy to vercel", "set up vercel", "add environment variables to vercel".

31.9K 116.1K
View

Coding & apps AI tools

Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View
I
Coding & apps

Imagica

Build a no-code AI app in minutes.

Freemium
View
E
Coding & apps

Emergent.sh

An IDE for code migration from legacy to modern frameworks through coding agents.

Freemium
View
Wonder Dynamics logo
Coding & apps

Wonder Dynamics

Automate CGI animation in live-action scenes

Paid
View
M
Coding & apps

Mixo

Launch a website in seconds with AI.

Paid
View
AI Code Convert logo
Coding & apps

AI Code Convert

Streamline Your Coding Experience with AI Code Helper

Free
View