ggen 4.0.0

ggen is a deterministic, language-agnostic code generation framework that treats software artifacts as projections of knowledge graphs.
# ggen Performance Documentation Hub

**Last Updated**: 2025-11-18
**Performance Grade**: **A-** (85/100)

## 📊 Quick Performance Status

| Component | Status | Performance |
|-----------|--------|-------------|
| **CLI Startup** | ✅ EXCELLENT | ~10ms (Target: <50ms) |
| **Memory Usage** | ✅ EXCELLENT | ~11MB (Target: <20MB) |
| **Template Parsing** | ✅ EXCELLENT | ~1-5ms (Target: <10ms) |
| **Template Caching** | ✅ EXCELLENT | Hit: <1ms, Miss: ~1-30ms |
| **RDF Operations** | ✅ GOOD | Insert: ~5-20ms, Query: ~0.1-100ms |
| **Lockfile Ops** | ⚠️ MONITOR | Load: ~20-50ms (100 entries) |
| **Code Generation** | ✅ GOOD | ~15-200ms (depends on RDF) |
| **Marketplace Search** | ✅ EXCELLENT | ~1-50ms (multi-tier cache) |

---

## 📚 Documentation Index

### For Developers

1. **[PERFORMANCE_QUICK_START.md]../PERFORMANCE_QUICK_START.md**
   - 5-minute quick start guide
   - How to run benchmarks
   - Common performance issues and fixes
   - **Start here** if you need fast results

2. **[PERFORMANCE_ANALYSIS.md]../PERFORMANCE_ANALYSIS.md**
   - Comprehensive 26KB performance analysis
   - Detailed bottleneck identification
   - Optimization roadmap with effort estimates
   - Performance targets and SLOs
   - **Read this** for deep understanding

3. **[PERFORMANCE_BENCHMARKING.md]../PERFORMANCE_BENCHMARKING.md**
   - Benchmarking methodology
   - How to add new benchmarks
   - Criterion usage guide
   - CI/CD integration patterns

### Performance Tools

4. **[Scripts: performance_benchmark.sh]../../scripts/performance_benchmark.sh**
   - Shell-based benchmarking suite
   - Fast baseline measurements
   - JSON result output
   - Run with: `./scripts/performance_benchmark.sh`

5. **[Benchmarks: performance_benchmark.rs]../../crates/ggen-core/benches/performance_benchmark.rs**
   - Criterion-based Rust benchmarks
   - Detailed performance metrics
   - Flamegraph support
   - Run with: `cargo bench --bench performance_benchmark`

---

## 🚀 Quick Commands

### Run All Performance Tests
```bash
# Quick shell benchmarks (30 seconds)
./scripts/performance_benchmark.sh

# Detailed Criterion benchmarks (5 minutes)
cargo bench --bench performance_benchmark

# With flamegraph profiling
cargo flamegraph --bench performance_benchmark
```

### Check Current Performance
```bash
# CLI startup time
time target/release/ggen --help

# Memory usage
/usr/bin/time -l target/release/ggen --version

# Template generation
time target/release/ggen generate test.tmpl
```

### Profile Hotspots
```bash
# CPU profiling (flamegraph)
cargo flamegraph --release --bin ggen -- generate template.tmpl

# Memory profiling (heaptrack - macOS)
heaptrack target/release/ggen generate template.tmpl
heaptrack_gui heaptrack.ggen.*.gz
```

---

## 🎯 Performance Targets

### Critical Operations

| Operation | Current | Target | Priority |
|-----------|---------|--------|----------|
| CLI startup | ~10ms | <50ms | ✅ Met |
| Template parse (simple) | ~1-5ms | <10ms | ✅ Met |
| Template render | ~5-50ms | <50ms | ⚠️ Monitor |
| RDF insert (small) | ~5-20ms | <50ms | ✅ Met |
| SPARQL query (cached) | ~0.1-1ms | <10ms | ✅ Met |
| Lockfile load (100) | ~20-50ms | <50ms | ⚠️ Monitor |
| Lockfile save (100) | ~30-80ms | <100ms | ✅ Met |
| Project scaffold (MVP) | Unknown | <2000ms | ⚠️ Needs measurement |

### Resource Limits

| Resource | Current | Limit | Status |
|----------|---------|-------|--------|
| Memory (CLI) | ~11MB | <20MB | ✅ Excellent |
| Memory (Graph) | ~20-50MB | <100MB | ✅ Good |
| Template Cache | ~1-5MB | <10MB | ✅ Excellent |
| Binary Size | ~5-10MB | <20MB | ✅ Excellent |

---

## 🔥 Top 3 Optimization Opportunities

### 1. Lazy RDF Loading (Quick Win)
**Impact**: 40-60% faster for non-RDF templates
**Effort**: 2-4 hours
**File**: `ggen-core/src/generator.rs`

```rust
// Skip RDF processing if template doesn't use it
if tmpl.has_graph_usage() {
    tmpl.process_graph(...)?;
}
```

### 2. Parallel Template Generation (Quick Win)
**Impact**: 2-4x faster for bulk generation
**Effort**: 2-4 hours
**File**: `ggen-core/src/templates/generator.rs`

```rust
use rayon::prelude::*;

templates.par_iter()
    .map(|t| generator.generate(t))
    .collect()
```

### 3. Optimize Lockfile Dependency Resolution (Medium Effort)
**Impact**: 50-80% faster lockfile operations
**Effort**: 1-2 days
**File**: `ggen-core/src/lockfile.rs`

```rust
// Parallel manifest loading
manifest.dependencies
    .par_iter()
    .map(|dep| load_manifest(dep))
    .collect()
```

**See [PERFORMANCE_ANALYSIS.md](../PERFORMANCE_ANALYSIS.md) Section 3 for full optimization roadmap**

---

## 📈 Performance Monitoring

### Key Metrics to Track

```rust
// Template operations
template_parse_duration_ms: Histogram  // Target p99 < 50ms
template_cache_hit_rate: Gauge         // Target > 70%

// RDF operations
rdf_insert_duration_ms: Histogram      // Target p99 < 100ms
sparql_query_duration_ms: Histogram    // Target p99 < 200ms

// Lockfile operations
lockfile_load_duration_ms: Histogram   // Target p99 < 100ms
lockfile_save_duration_ms: Histogram   // Target p99 < 200ms

// System resources
memory_usage_bytes: Gauge              // Target < 100MB
cpu_usage_percent: Gauge               // Target < 80%
```

### Instrumentation Points

Add OpenTelemetry spans to critical paths:

```rust
use tracing::{instrument, info_span};

#[instrument(skip(self))]
pub fn generate(&mut self) -> Result<PathBuf> {
    let _parse_span = info_span!("template_parse").entered();
    // ... parsing logic ...

    let _render_span = info_span!("template_render").entered();
    // ... rendering logic ...
}
```

---

## 🛠️ Development Workflow

### Before Committing Performance Changes

1. **Run baseline benchmarks**
   ```bash
   cargo bench -- --save-baseline before
   ```

2. **Make your changes**

3. **Run comparison benchmarks**
   ```bash
   cargo bench -- --baseline before
   ```

4. **Verify improvements**
   - Check that p99 latency improved
   - Ensure memory usage didn't increase
   - Run `cargo test` to verify correctness

5. **Update documentation**
   - Add findings to PERFORMANCE_ANALYSIS.md
   - Update this README if targets changed

### CI/CD Integration

Performance tests run automatically on PRs:

```yaml
# .github/workflows/performance.yml
- name: Run benchmarks
  run: cargo bench --bench performance_benchmark

- name: Check for regressions
  run: |
    cargo bench -- --baseline main --test
```

---

## 🐛 Common Performance Issues

### Issue 1: Slow Template Generation (>500ms)

**Diagnosis**:
```bash
# Check for RDF blocks
grep -E "graph:|query:" template.tmpl

# Profile with flamegraph
cargo flamegraph --release --bin ggen -- generate template.tmpl
```

**Solutions**:
- Apply lazy RDF loading optimization
- Enable template caching
- Simplify SPARQL queries

### Issue 2: High Memory Usage (>100MB)

**Diagnosis**:
```bash
/usr/bin/time -l target/release/ggen generate template.tmpl
# Check "maximum resident set size"
```

**Solutions**:
- Reduce template cache size
- Clear graph after generation
- Use streaming for large files

### Issue 3: Slow Lockfile Operations (>1s)

**Diagnosis**:
```bash
# Count dependencies
cat ggen.lock | grep "id =" | wc -l

# Profile
cargo flamegraph --release --bin ggen -- pack install <package>
```

**Solutions**:
- Apply parallel dependency resolution
- Use lazy loading for dependencies
- Consider binary lockfile format

**See [PERFORMANCE_QUICK_START.md](../PERFORMANCE_QUICK_START.md) Section 4 for detailed fixes**

---

## 📊 Benchmark Results Archive

### Latest Results (2025-11-18)

**Platform**: macOS Darwin 24.5.0
**CPU**: Apple Silicon (M-series)
**Build**: Release with optimizations

| Benchmark | Result | Status |
|-----------|--------|--------|
| CLI startup (--help) | 10ms ||
| Memory usage (--version) | 11MB ||
| Template parse (simple) | 1.2ms ± 0.1ms ||
| Template cache hit | 0.8ms ± 0.05ms ||
| RDF insert (small) | 12ms ± 2ms ||
| SPARQL query (cached) | 0.5ms ± 0.1ms ||
| Lockfile load (100 entries) | 45ms ± 5ms | ⚠️ |

**Full results**: `perf_results/benchmark_YYYYMMDD_HHMMSS.json`

---

## 🤝 Contributing Performance Improvements

### Adding New Benchmarks

1. Add to `crates/ggen-core/benches/performance_benchmark.rs`:
   ```rust
   fn bench_new_operation(c: &mut Criterion) {
       c.bench_function("new_operation", |b| {
           b.iter(|| {
               // Your benchmark code
           })
       });
   }

   criterion_group!(benches, bench_new_operation);
   ```

2. Run and verify:
   ```bash
   cargo bench --bench performance_benchmark
   ```

3. Document in PERFORMANCE_ANALYSIS.md

### Optimization Guidelines

1. **Profile first** - Don't optimize without data
2. **Set targets** - Know your performance goals
3. **Measure impact** - Quantify improvements
4. **Consider trade-offs** - Don't sacrifice correctness for speed
5. **Document** - Explain why and what you optimized

---

## 📖 External Resources

- [Rust Performance Book]https://nnethercote.github.io/perf-book/
- [Criterion.rs User Guide]https://bheisler.github.io/criterion.rs/book/
- [Flamegraph Interpretation Guide]http://www.brendangregg.com/flamegraphs.html
- [Oxigraph Performance Tuning]https://github.com/oxigraph/oxigraph/wiki/Performance

---

## 📞 Support

**Questions about performance?**
- Create an issue with label `performance`
- Include benchmark results and flamegraph
- Tag `@performance-team`

**Found a performance regression?**
- Run `cargo bench` to confirm
- Bisect to find the offending commit
- Create PR with fix and regression test

---

## 📅 Performance Review Schedule

- **Weekly**: Review benchmark CI results
- **Monthly**: Run full performance analysis
- **Quarterly**: Update performance targets and roadmap
- **Next Review**: 2025-12-18

---

**Maintained by**: Performance Benchmarker Agent
**Document Version**: 1.0
**Performance Tracking Dashboard**: [Link TBD]