Overview
MC-CPP includes dedicated profiling benchmarks to measure performance-critical operations. The test suite identifies hotspots and validates optimization strategies.Benchmark Architecture
Benchmarks usestd::chrono::high_resolution_clock for precise timing measurements:
Chunk Meshing Benchmarks
Implementation (tests/perf_bench.cpp)
Measures mesh generation performance for different chunk densities.
Helper: Block Type Setup
Test World Builder
Dense vs Sparse Filling
(x + y + z) % 31 == 0 pattern
Mesh Rebuilding Benchmark
- Dense chunks: Higher mesh generation time (more visible faces)
- Sparse chunks: Lower time (most blocks culled by neighbors)
Usage
Block Operations Benchmark
Set/Remove Block Performance
- Opaque block placement/removal (block_id = 1)
- Light source placement/removal (block_id = 10)
- Block data update time
- Lighting queue population
- Mesh invalidation overhead
Usage
Hotspot Analysis (tests/perf_hotspots.cpp)
Chunk Sorting Optimization
Compares distance calculation methods for chunk rendering order.Method 1: Direct Distance with sqrt
glm::distance computes sqrt(dx² + dy² + dz²) for every comparison
Method 2: Squared Distance (Optimized)
sqrt is monotonic, dist²(a) > dist²(b) ⟺ dist(a) > dist(b)
Mesh Merging Optimization
Compares vector concatenation strategies for merging subchunk meshes.Without Reserve (Naive)
With Reserve (Optimized)
Running Hotspot Tests
Building and Running Tests
Compilation
Expected Output
perf_bench.cpp:Profiling Workflow
1. Identify Hotspot
Use external profilers for real-time analysis: Linux:perf or valgrind --tool=callgrind
2. Write Isolated Benchmark
Extract suspected hotspot into standalone test:3. Test Optimization
Compare baseline vs optimized implementation:4. Validate in Production
Confirm improvement in actual gameplay:- Monitor frame times during chunk loading
- Test with various
RENDER_DISTANCEsettings - Verify no visual regressions
Performance Metrics
Target Frame Budget (60 FPS)
- Total frame time: 16.67 ms
- Chunk mesh updates: < 2 ms
- Lighting propagation: < 1 ms
- Rendering: < 10 ms
- Input/physics: < 2 ms
- Overhead: < 2 ms
Bottleneck Identification
Common symptoms: CPU-bound:- High frame time with low GPU utilization
- Hotspots: mesh generation, lighting BFS, sorting
- Solution: Reduce
CHUNK_UPDATES,LIGHT_STEPS_PER_TICK
- GPU at 100% utilization
- Hotspots: fragment shader complexity, overdraw
- Solution: Reduce
RENDER_DISTANCE, disableSMOOTH_LIGHTING, lowerANTIALIASING
- Stuttering during chunk load/unload
- Hotspots: texture uploads, VBO uploads
- Solution: Reduce
RENDER_DISTANCE, optimize atlas packing
Adding New Benchmarks
Template
Best Practices
- Warmup runs: Execute operation once before timing to warm caches
- Multiple iterations: Average across many runs to reduce noise
- Representative data: Use realistic data sizes and distributions
- Isolated testing: Minimize external dependencies
- Consistent environment: Close other applications, disable CPU throttling
- Statistical validation: Report min/max/stddev for stability analysis