/optimize

Development L3 Production medium usage

/optimize

Overview

/optimize is a production command in the Development category of the Prismatic Platform. It performs performance optimization with measurement validation, ensuring that every optimization claim is backed by quantitative evidence from before-and-after benchmarking, profiling data, and regression verification.

Performance optimization in the Prismatic Platform follows a disciplined methodology that rejects premature optimization and gut-feeling improvements. The /optimize command enforces this discipline by requiring baseline measurements before any changes, automated benchmarking after changes, and statistical validation that improvements are genuine rather than measurement noise. This evidence-based approach prevents the common antipattern of "optimization" that degrades code clarity without measurable benefit.

The command integrates with the platform's comprehensive telemetry infrastructure to capture performance data at multiple granularity levels: function-level execution time, module-level throughput, application-level latency, and system-level resource utilization. All measurements are stored in the platform's time-series database for historical trend analysis and regression detection.

The platform enforces hard performance limits: all pages must load under 250ms, server-side rendering must complete under 100ms, LiveView mounts under 150ms, and handle_event callbacks under 50ms. The /optimize command is the primary tool for achieving and maintaining compliance with these standards.

This command operates under the L3 authority level and is executed by the performance-specialist agent. It is part of the platform's 216-command slash command registry, built on the AIAD (Autonomous Intelligence Agent Design) standard.

Architecture

The /optimize command implements a measurement-driven optimization pipeline built on Benchee for microbenchmarks and custom telemetry for application-level measurements.

Optimization Pipeline

Target Identification --> Baseline Measurement
                               |
                         Profile Analysis
                               |
                    Optimization Strategy Selection
                               |
                    +----------+----------+
                    |          |          |
               Algorithm   Memory    Concurrency
               Optimization Reduction Tuning
                    |          |          |
                    +----------+----------+
                               |
                    Post-Optimization Measurement
                               |
                    Statistical Validation
                               |
                    Regression Verification
                               |
                    Report Generation

Core Components

ComponentModuleResponsibility
Baseline CollectorPrismaticPerformance.BaselineCollectorPre-optimization measurement capture
ProfilerPrismaticPerformance.Profiler:fprof, :eprof, :cprof integration
Strategy SelectorPrismaticPerformance.StrategySelectorOptimization approach recommendation
Benchmark RunnerPrismaticPerformance.BenchmarkRunnerBenchee-based measurement execution
ValidatorPrismaticPerformance.StatisticalValidatorStatistical significance testing
Regression GuardPrismaticPerformance.RegressionGuardRegression detection across test suite

Usage

Basic Optimization

# Optimize a specific module
/optimize --target PrismaticPerimeter.SecurityRating

# Optimize a specific function
/optimize --target PrismaticPerimeter.SecurityRating.calculate/2

# Optimize with automatic profiling
/optimize --target PrismaticWeb.PerimeterLive --profile auto

# Quick optimization check (baseline + suggestions only)
/optimize --target PrismaticStorage.Query --mode analyze

Performance Profiling

# Full profile with flame graph
/optimize --target PrismaticPerimeter --profile full --flame-graph

# Memory profiling
/optimize --target PrismaticStorage.ETS --profile memory

# Concurrency profiling
/optimize --target PrismaticAgents.Orchestrator --profile concurrency

# IO profiling
/optimize --target PrismaticStorage.Ecto --profile io

Benchmarking

# Run benchmarks with statistical analysis
/optimize --target PrismaticPerimeter.Scanner --benchmark --iterations 1000

# Compare implementations
/optimize --compare "v1:PrismaticPerimeter.Scanner.scan_v1/1" "v2:PrismaticPerimeter.Scanner.scan_v2/1"

# Benchmark against performance budget
/optimize --target PrismaticWeb.PerimeterLive --benchmark --budget 100ms

System-Wide Optimization

# Optimize all modules in an application
/optimize --app prismatic_perimeter --mode sweep

# Optimize based on production telemetry hotspots
/optimize --source telemetry --top 10

# Optimize ETS table configurations
/optimize --target ets-tables --mode memory-layout

Options & Parameters

ParameterTypeDefaultDescription
--targetStringRequiredModule, function, or application to optimize
--appString-Target umbrella application
--modeEnumoptimizeMode: analyze, optimize, sweep, verify
--profileEnumautoProfiler: auto, fprof, eprof, cprof, memory, concurrency, io, full
--benchmarkBooleanfalseRun Benchee benchmarks
--iterationsInteger100Benchmark iteration count
--warmupDuration2sBenchmark warmup time
--budgetDuration-Performance budget target
--compareList-Compare two or more implementations
--flame-graphBooleanfalseGenerate flame graph visualization
--sourceEnumcodeData source: code, telemetry, benchmarks
--topInteger5Number of hotspots to analyze
--min-improvementFloat0.1Minimum improvement ratio to accept (10%)
--outputEnumtextOutput format: text, json, html, markdown
--filePath-Write results to file

Execution Flow

Phase 1 -- Target Analysis (0-5s): The target module or function is analyzed for its current structure, complexity, and integration points. The analyzer identifies potential optimization targets including hot paths, memory allocation patterns, and concurrency bottlenecks.

Phase 2 -- Baseline Measurement (5-30s): A comprehensive baseline is established through automated benchmarking. The baseline captures execution time (mean, median, P95, P99), memory allocation, garbage collection frequency, and process message queue depth. Multiple measurement runs ensure statistical reliability.

Phase 3 -- Profiling (30-60s): The selected profiler instruments the target code and captures detailed execution traces. For :fprof, this includes call counts, accumulated time, and own time for every function in the call tree. Results are analyzed to identify the most impactful optimization candidates.

Phase 4 -- Strategy Selection (60-65s): Based on profiling data, the Strategy Selector recommends optimization approaches ranked by expected impact. Common strategies include algorithm replacement, caching, concurrency adjustment, data structure optimization, and lazy evaluation.

Phase 5 -- Optimization Implementation (65-variable): The performance-specialist agent implements the recommended optimizations. Each optimization is applied incrementally with intermediate measurements to validate impact.

Phase 6 -- Post-Optimization Measurement (variable): The same benchmark suite from the baseline phase is re-executed against the optimized code. Results are compared using statistical tests (t-test or Mann-Whitney U) to determine if improvements are statistically significant.

Phase 7 -- Regression Verification (variable): The full test suite is executed to verify that optimizations have not introduced functional regressions. Any test failure aborts the optimization and reverts changes.

Phase 8 -- Report Generation (2-5s): A detailed optimization report is generated including before/after measurements, improvement percentages, statistical confidence, and recommendations for further optimization.

Integration Points

ComponentIntegration TypeDescription
Prismatic AgentsExecutionManaged by performance-specialist agent
Quality GatesPre/post validationPerformance gates as quality checkpoints
TelemetryData sourceProduction metrics for hotspot identification
BencheeMeasurementStatistical benchmarking framework
Test SuiteRegression guardFunctional verification after optimization
CI/CD PipelineGatePerformance budgets enforced in pipeline
Page Load PolicyStandards250ms page load, 100ms render targets

Best Practices

Measure First: Never optimize without baseline measurements. The /optimize --mode analyze flag provides profiling data and optimization suggestions without making any changes. Use this to identify whether optimization is warranted before investing effort.

Statistical Rigor: A single benchmark run is not evidence of improvement. Use --iterations 1000 or higher for microbenchmarks. The statistical validator will reject improvements that are not statistically significant at the 95% confidence level.

Optimize Bottlenecks: Focus on the hottest code paths identified by profiling. Optimizing code that consumes 1% of execution time cannot produce meaningful overall improvement regardless of how much faster it becomes.

Preserve Clarity: Reject optimizations that reduce code readability without providing at least 10% improvement (configurable via --min-improvement). Maintainability is a long-term performance characteristic that premature optimization degrades.

Regression Testing: Always verify the full test suite after optimization. Performance improvements that break functionality are not improvements. The platform's mandatory regression test protocol applies to optimization changes.

Error Handling

Error ConditionHandling StrategyUser Impact
Target not foundError with module/function suggestionsCommand does not execute
Baseline instabilityIncreased warmup and iterations automaticallyLonger baseline phase
Profiler overhead too highSwitch to lighter profilerLess detailed profiling data
Optimization causes test failureAutomatic revert, failure reportChanges rolled back
Improvement not statistically significantReported as inconclusiveNo changes applied
Memory limit during profilingProfiling scope reducedPartial profiling results

Advanced Usage

Continuous Performance Monitoring

# Set up performance monitoring for a module
/optimize --target PrismaticWeb.PerimeterLive --mode monitor --alert-threshold 200ms

# Review performance trends
/optimize --trends --target PrismaticPerimeter --period 30d

A/B Implementation Testing

# Compare two implementations with statistical rigor
/optimize --ab-test \
  --control PrismaticPerimeter.Scanner.scan_sequential/1 \
  --variant PrismaticPerimeter.Scanner.scan_concurrent/1 \
  --iterations 5000 --confidence 0.99

O(1) Pattern Detection

The platform has achieved O(1) pattern detection providing 90-250x speedup over traditional approaches. Verify and maintain this performance:

# Verify O(1) detection performance
/optimize --verify-o1 --target PrismaticSafety.PatternDetector --input-sizes "100,1000,10000,100000"

Doctrine Compliance

All commands operate under the NO MERCY, NO DOUBTS doctrine:

  • NO MERCY: Zero tolerance for incomplete execution or quality violations. Every optimization must be fully measured, validated, and regression-tested. No "it feels faster" claims. No optimization without evidence.
  • NO DOUBTS: Full investigation before action, evidence-based results. Profiling before optimizing. Statistical validation of improvements. Quantitative reporting with confidence intervals.
  • /perf-profile - Application profiling and performance hotspot identification
  • /code - Core coding implementation and feature development
  • /fix - Bug fix implementation with mandatory regression tests
  • /test - Comprehensive test generation and verification
  • /quality-gates - Enforce quality gate checkpoints with zero-warning compilation validation
  • /regression-check - Execute 25 custom Credo regression checks preventing 700+ violations

Connect & Contribute

Created by Tomáš Korcak (korczis) | Open Source under GHL