Skip to content

Instantly share code, notes, and snippets.

@PriNova
Created June 26, 2025 07:01
Show Gist options
  • Select an option

  • Save PriNova/644a50202c0b97798a820b41c9bc3499 to your computer and use it in GitHub Desktop.

Select an option

Save PriNova/644a50202c0b97798a820b41c9bc3499 to your computer and use it in GitHub Desktop.
Performance Analysis

How to Perform Critical Performance Analysis Using Sub-Agents

Overview

This workflow provides comprehensive performance analysis for code changes with the rigor expected in game engines, graphics engines, and critical systems where speed, space efficiency, reliability, and robustness are paramount. The approach systematically evaluates algorithmic complexity, data structures, memory patterns, and system-level optimizations through specialized sub-agents with deep performance expertise.

Complete Critical Performance Analysis Workflow Prompt

I need to perform critical performance analysis for [CODE CHANGE DESCRIPTION] in [REPOSITORY/BRANCH]. This analysis must meet the standards of game engines, graphics engines, and critical systems where performance is paramount. Follow this comprehensive performance analysis workflow with nested sub-agent delegation:

**PHASE 0 - PERFORMANCE SCOPE ASSESSMENT (Main Agent):**
1. Check existing knowledge graph for performance patterns and previous optimization work
2. Analyze code changes to identify performance-critical areas: algorithms, data structures, memory access patterns, I/O operations
3. Determine baseline performance requirements and acceptable performance budgets
4. Identify critical path operations, hot code paths, and real-time constraints
5. Create initial todo breakdown using todo_write with performance priorities

**PHASE 1 - MULTI-DOMAIN PERFORMANCE ANALYSIS (6 Parallel Sub-Agents):**
6. Deploy SIX parallel sub-agents simultaneously for comprehensive performance domain coverage:
   - Algorithm Analysis Sub-Agent: Analyze time complexity (Big-O), space complexity, algorithmic efficiency, and optimization opportunities
   - Data Structure Analysis Sub-Agent: Evaluate data structure choices, access patterns, cache efficiency, and memory layout optimization
   - Memory Performance Sub-Agent: Analyze memory allocation patterns, garbage collection impact, memory fragmentation, and resource lifecycle management
   - I/O Performance Sub-Agent: Evaluate file system operations, network I/O, database queries, and I/O blocking patterns
   - Concurrency Performance Sub-Agent: Analyze thread safety, lock contention, parallel processing efficiency, and synchronization overhead
   - System Resource Sub-Agent: Evaluate CPU utilization, memory bandwidth, cache miss rates, and system call overhead
7. Main Agent: Process domain findings and create Agent Briefing for Phase 1B
8. **CHECKPOINT 1**: If checkpoints enabled, present performance domain analysis and critical issue identification

**PHASE 1B - NESTED CRITICAL PERFORMANCE ANALYSIS (Selected Sub-Agents Deploy Sub-Sub-Agents):**
9. Based on Phase 1 findings, deploy NESTED SUB-AGENTS for critical performance bottlenecks requiring deep analysis:
   - **Algorithm Sub-Agent** → Deploy 3 nested agents: Complexity Analysis (Big-O verification), Loop Optimization Review, Recursive Algorithm Analysis
   - **Data Structure Sub-Agent** → Deploy 3 nested agents: Cache Locality Analysis, Memory Access Pattern Review, Data Structure Efficiency Assessment
   - **Memory Sub-Agent** → Deploy 3 nested agents: Allocation Pattern Analysis, Memory Pool Assessment, Garbage Collection Impact Review
   - **I/O Sub-Agent** → Deploy 3 nested agents: Blocking Operation Analysis, Asynchronous I/O Review, Batching Optimization Assessment
   - **Concurrency Sub-Agent** → Deploy 3 nested agents: Lock-Free Algorithm Review, Thread Pool Analysis, Synchronization Overhead Assessment
   - **System Resource Sub-Agent** → Deploy 3 nested agents: CPU Pipeline Analysis, Memory Bandwidth Assessment, System Call Optimization Review
10. Main Agent: Synthesize nested performance analysis results and create Agent Briefing for Phase 2
11. **CHECKPOINT 1B**: If checkpoints enabled, present deep performance analysis findings and optimization recommendations

**PHASE 2 - PERFORMANCE INTEGRATION ANALYSIS (Main Agent + 1 Sub-Agent):**
12. Main Agent: Synthesize findings from all performance domains, identify performance interaction effects and system-wide impacts
13. Deploy ONE sub-agent to receive comprehensive Agent Briefing and analyze: performance trade-offs between different optimizations, system-level performance interactions, end-to-end performance impact assessment
14. Main Agent: Create Agent Briefing for Phase 3 with performance risk assessment and optimization priorities
15. **CHECKPOINT 2**: If checkpoints enabled, present integrated performance analysis and optimization strategy

**PHASE 3 - PERFORMANCE OPTIMIZATION RECOMMENDATIONS (4 Parallel Sub-Agents):**
16. Deploy FOUR parallel sub-agents for optimization strategy development:
    - Critical Bottleneck Sub-Agent: Receive Agent Briefing, identify performance-blocking issues requiring immediate attention before deployment
    - Space-Time Optimization Sub-Agent: Receive Agent Briefing, recommend space-time trade-offs, memory vs. CPU optimizations, and algorithmic improvements
    - Scalability Enhancement Sub-Agent: Receive Agent Briefing, assess performance under load, concurrent access patterns, and scalability bottlenecks
    - Implementation Strategy Sub-Agent: Receive Agent Briefing, provide specific implementation guidance for performance optimizations with measurable targets
17. Main Agent: Validate optimization recommendations, prioritize by performance impact, create Agent Briefing for Phase 4
18. **CHECKPOINT 3**: If checkpoints enabled, review optimization strategy and implementation approach

**PHASE 4 - PERFORMANCE VERIFICATION STRATEGY (3 Sequential Sub-Agents):**
19. Deploy THREE sub-agents sequentially for comprehensive performance validation:
    - Benchmarking Sub-Agent: Receive Agent Briefing, design performance benchmarks, profiling strategies, and measurement methodologies
    - Testing Sub-Agent: Receive Agent Briefing, create performance regression tests, load testing scenarios, and stress testing approaches
    - Monitoring Sub-Agent: Receive Agent Briefing, establish performance monitoring, alerting thresholds, and continuous performance tracking
20. Main Agent: Compile comprehensive performance analysis report, update knowledge graph with performance patterns

**WORKFLOW-SPECIFIC DELEGATION:**
- **Main Agent**: Performance scope assessment, Agent Briefing management, cross-domain synthesis, final optimization strategy
- **Performance Domain Sub-Agents**: Deep analysis within specialized performance areas (algorithms, memory, I/O, etc.)
- **Nested Performance Sub-Agents**: Expert-level analysis of specific performance bottlenecks and optimization techniques
- **Integration Sub-Agent**: System-wide performance impact analysis and optimization interaction assessment
- **Optimization Sub-Agents**: Strategic performance improvement recommendations with implementation guidance
- **Verification Sub-Agents**: Performance validation, testing, and continuous monitoring strategy

**CRITICAL PERFORMANCE SCOPE ISOLATION:**
- **Each domain sub-agent analyzes only their performance area** (e.g., algorithm efficiency, memory patterns) and ignores other domains
- **Nested sub-agents focus on specific technical performance issues** within their parent domain without considering other areas
- **System-wide performance interactions are handled by integration sub-agent** after domain analysis is complete
- **No sub-agent attempts overall performance assessment** - that's the main agent's coordination responsibility

**NESTED SUB-AGENT DEPLOYMENT CRITERIA:**
- **Deploy algorithm nesting**: When complex algorithms, recursive operations, or mathematical computations detected
- **Deploy data structure nesting**: When custom data structures, complex collections, or memory-intensive operations detected
- **Deploy memory nesting**: When dynamic allocation, large object creation, or memory-intensive operations detected
- **Deploy I/O nesting**: When file operations, network calls, database interactions, or blocking operations detected
- **Deploy concurrency nesting**: When threading, parallel processing, or synchronization mechanisms detected
- **Deploy system resource nesting**: When low-level operations, system calls, or hardware interaction detected

**PERFORMANCE ANALYSIS STANDARDS:**
- **Game Engine Quality**: Sub-millisecond response times, 60+ FPS maintenance, minimal garbage collection impact
- **Graphics Engine Quality**: Real-time rendering constraints, memory bandwidth optimization, GPU utilization efficiency
- **Critical System Quality**: Predictable performance, bounded worst-case behavior, resource consumption limits
- **Reliability Requirements**: Performance consistency under load, graceful degradation, resource leak prevention
- **Robustness Standards**: Performance isolation, error recovery without performance impact, monitoring and alerting

**KNOWLEDGE ENHANCEMENT:**
- Store performance optimization patterns, algorithmic improvements, and effective optimization techniques for future analysis
- Build expertise in codebase-specific performance characteristics and optimization opportunities
- Learn from performance testing results to improve future analysis accuracy and optimization recommendations

**HUMAN FEEDBACK OPTIONS:**
- **Default**: Fully autonomous execution with comprehensive performance analysis report
- **Add "WITH CHECKPOINTS"**: Pause after each major phase for review and guidance on optimization priorities
- **Add "REVIEW ANALYSIS"**: Pause only after Phase 1B to confirm critical performance findings and optimization focus
- **Add "REVIEW OPTIMIZATIONS"**: Pause only after Phase 3 to review optimization recommendations before verification strategy

**CHECKPOINT BEHAVIOR:**
When pausing, provide:
1. Clear summary of performance analysis findings with quantified impact assessments
2. Domain-specific performance bottlenecks with severity and optimization potential
3. Identified critical performance issues requiring immediate attention
4. Proposed optimization strategies with expected performance improvements
5. Option to continue autonomously or adjust performance analysis focus areas

**CODE CHANGES TO ANALYZE:**
[CODE CHANGE DESCRIPTION] in [REPOSITORY/BRANCH] [OPTIONAL: WITH CHECKPOINTS | REVIEW ANALYSIS | REVIEW OPTIMIZATIONS]

Begin with Phase 0 performance scope assessment and proceed systematically through each phase with critical system performance standards.

Usage Instructions

  1. Replace placeholders with specific information:

    • [CODE CHANGE DESCRIPTION] - Description of performance-critical changes (e.g., "New sorting algorithm implementation", "Database query optimization", "Real-time rendering pipeline")
    • [REPOSITORY/BRANCH] - Specific repository and branch being analyzed (e.g., "game-engine/optimization-branch")
  2. Choose feedback level by adding optional modifiers:

    • No modifier = fully autonomous performance analysis
    • WITH CHECKPOINTS = pause after each major phase for human input on optimization priorities
    • REVIEW ANALYSIS = pause only after deep analysis to confirm critical performance findings
    • REVIEW OPTIMIZATIONS = pause only after optimization recommendations to review strategy
  3. Ensure performance context is available with baseline measurements, performance requirements, and critical path identification

  4. Prepare for detailed output - this workflow generates comprehensive performance reports with quantified optimization recommendations

Key Benefits

  • Critical System Standards: Analysis meets game engine, graphics engine, and mission-critical system performance requirements
  • Comprehensive Coverage: Six specialized performance domains ensure no optimization opportunity is missed
  • Deep Technical Analysis: Nested sub-agents provide expert-level performance optimization expertise
  • Algorithmic Focus: Systematic Big-O analysis, complexity evaluation, and algorithmic efficiency assessment
  • Memory Optimization: Detailed memory access pattern analysis, cache efficiency, and allocation optimization
  • Quantified Recommendations: Performance improvements with measurable targets and expected impact
  • System-Level Perspective: Cross-domain performance interactions and end-to-end optimization strategy

Performance Domain Examples

Algorithm Analysis Focus:

  • Time complexity (Big-O) verification and optimization opportunities
  • Space complexity assessment and memory usage optimization
  • Loop optimization, unrolling, and vectorization potential
  • Recursive algorithm analysis and tail recursion optimization
  • Mathematical algorithm efficiency and numerical stability

Data Structure Analysis Focus:

  • Cache locality optimization and memory access pattern improvement
  • Data structure selection for optimal performance characteristics
  • Memory layout optimization for CPU cache efficiency
  • Collection access pattern analysis and iteration optimization
  • Custom data structure design for performance-critical operations

Memory Performance Focus:

  • Allocation pattern analysis and memory pool optimization
  • Garbage collection impact assessment and mitigation strategies
  • Memory fragmentation analysis and prevention techniques
  • Object lifecycle management and resource cleanup optimization
  • Memory bandwidth utilization and access pattern optimization

I/O Performance Focus:

  • Blocking operation identification and asynchronous conversion opportunities
  • File system operation optimization and caching strategies
  • Network I/O batching, compression, and protocol optimization
  • Database query optimization and connection pool management
  • Buffer management and I/O scheduling optimization

Concurrency Performance Focus:

  • Lock contention analysis and lock-free algorithm opportunities
  • Thread pool optimization and work distribution strategies
  • Synchronization overhead assessment and optimization
  • Parallel algorithm design and scalability analysis
  • NUMA-aware optimization and CPU affinity strategies

System Resource Focus:

  • CPU pipeline optimization and instruction-level parallelism
  • Memory bandwidth utilization and cache miss optimization
  • System call overhead analysis and batching opportunities
  • Hardware-specific optimization (SIMD, GPU utilization)
  • Power consumption and thermal management considerations

Agent Briefing Examples

Phase 1 → Phase 1B Agent Briefing:

**AGENT BRIEFING FROM PERFORMANCE DOMAIN ANALYSIS:**

**Critical Performance Areas Requiring Deep Analysis:**
- Algorithm: O(n²) sorting operation detected in hot path - needs optimization to O(n log n)
- Memory: Large object allocation in tight loop - potential for memory pool optimization
- I/O: Synchronous file operations blocking main thread - async conversion opportunity

**Performance Domain Findings Summary:**
Algorithm: Bubble sort implementation in data processing pipeline, nested loop complexity issues
Memory: Dynamic allocation of 1MB objects every frame, potential memory fragmentation
I/O: File loading operations on main thread causing 50ms+ frame drops
Concurrency: Single-threaded processing with parallelization opportunities
System: CPU cache misses due to random memory access patterns

**Critical Path Analysis:**
- Hot Path 1: Data sorting in game loop (60 FPS requirement)
- Hot Path 2: Asset loading during gameplay (real-time constraint)
- Hot Path 3: Physics calculations (sub-millisecond requirement)

**Nested Analysis Priorities:**
1. Algorithm → Loop optimization and complexity reduction (critical - performance blocking)
2. Memory → Allocation pattern optimization (high priority - stability impact)
3. I/O → Asynchronous operation conversion (medium priority - user experience)

**Performance Targets:**
- Maintain 60 FPS during all operations
- Reduce memory allocation by 80%
- Eliminate blocking operations in main thread

Phase 2 → Phase 3 Agent Briefing:

**AGENT BRIEFING FROM PERFORMANCE INTEGRATION ANALYSIS:**

**System-Wide Performance Interactions:**
- Algorithm optimization reduces CPU load but increases memory usage
- Memory pooling improves allocation performance but affects cache locality
- Async I/O improves responsiveness but adds complexity to error handling

**End-to-End Performance Impact:**
- Current: 45 FPS average with 200ms loading times and 512MB memory usage
- Potential: 60+ FPS sustained with 50ms loading times and 256MB memory usage
- Critical: Frame rate consistency more important than peak performance

**Optimization Trade-Off Analysis:**
- Space-Time: Accept 20% memory increase for 40% CPU performance improvement
- Complexity-Performance: Async patterns add maintenance cost but eliminate blocking
- Reliability-Speed: Conservative memory management vs. aggressive optimization

**Performance Risk Assessment:**
- High Risk: Algorithm changes affect game logic correctness
- Medium Risk: Memory optimization may impact garbage collection patterns
- Low Risk: I/O changes are isolated and well-testable

**Optimization Priority Matrix:**
1. Critical: Algorithm optimization (blocking 60 FPS requirement)
2. High: Memory allocation optimization (stability and consistency)
3. Medium: I/O async conversion (user experience improvement)
4. Low: System-level optimizations (diminishing returns)

Performance Analysis Best Practices

  • Measure First: Establish baseline performance metrics before optimization
  • Profile Systematically: Use profiling tools to identify actual bottlenecks, not assumed ones
  • Focus on Critical Path: Optimize operations in performance-critical execution paths first
  • Consider Real-World Conditions: Analyze performance under realistic load and data conditions
  • Validate Optimizations: Measure performance improvements to confirm optimization effectiveness
  • Balance Trade-offs: Consider space-time, complexity-performance, and reliability-speed trade-offs
  • Maintain Consistency: Ensure performance optimizations don't introduce performance regressions elsewhere
  • Document Assumptions: Record performance assumptions and constraints for future maintenance

Common Performance Pitfalls Avoided

  • Premature Optimization: Focus analysis on actual performance bottlenecks identified through profiling
  • Micro-Optimization Tunnel Vision: Consider system-wide performance impact, not just local optimizations
  • Algorithm Complexity Ignorance: Systematic Big-O analysis prevents scalability problems
  • Memory Pattern Blindness: Comprehensive memory analysis prevents allocation performance issues
  • Concurrency Naive Assumptions: Expert analysis of threading and synchronization overhead
  • I/O Blocking Oversight: Systematic identification of blocking operations in critical paths
  • Cache Inefficiency: Memory access pattern analysis optimizes CPU cache utilization
  • Performance Regression Introduction: Integration analysis catches optimization conflicts

Critical System Performance Standards

  • Real-Time Constraints: Meet hard deadlines for time-critical operations
  • Predictable Performance: Bounded worst-case execution times and consistent behavior
  • Resource Efficiency: Optimal utilization of CPU, memory, and I/O resources
  • Scalability Assurance: Performance maintains under increased load and data volume
  • Robustness Under Stress: Graceful degradation and recovery from performance stress
  • Monitoring and Observability: Comprehensive performance tracking and alerting capabilities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment