Why Profile the Compiler?
Compilation Speed
Reduce time spent compiling Rust code
Memory Usage
Minimize compiler memory consumption
Query Optimization
Optimize the query system for better caching
Code Quality
Improve generated code performance
Profiling Setup
Self-Profiling with measureme
Rustc has built-in self-profiling using themeasureme framework:
Basic Self-Profiling
- Enable Self-Profiling
- Profile Events
- Analyze Results
Analyzing Self-Profile Data
Time-Passes Profiling
Track time spent in each compiler pass:- Basic Time Tracking
- With LLVM Timing
- Pretty Printing
CPU Profiling
Using perf (Linux)
Using Instruments (macOS)
Using samply (Cross-Platform)
- Basic Profiling
- With Arguments
Memory Profiling
Heap Profiling with Valgrind
- Massif
- DHAT
Memory Statistics
Query Performance Analysis
Query Profiling
The compiler’s query system is a major performance factor:- Query Statistics
- Dependency Graph
- Query Blocking
Incremental Compilation
LLVM Performance
LLVM Pass Timing
- Time LLVM Passes
- Optimization Remarks
- LLVM Stats
LLVM Profiling
Benchmarking
Compiler Benchmarks
Custom Benchmarks
Performance Analysis Workflow
Common Performance Issues
Slow query execution
Slow query execution
Symptoms: High time in specific queriesDiagnosis:Solutions:
- Add caching
- Optimize hot paths
- Reduce query invocations
Poor cache hit rate
Poor cache hit rate
Symptoms: Low query cache hitsDiagnosis:Solutions:
- Improve query key design
- Enable incremental compilation
- Reduce query invalidation
High memory usage
High memory usage
Symptoms: Excessive memory consumptionDiagnosis:Solutions:
- Reduce type sizes
- Optimize data structures
- Free unused memory
Slow LLVM codegen
Slow LLVM codegen
Symptoms: Long time in codegen phaseDiagnosis:Solutions:
- Reduce codegen units
- Optimize MIR
- Disable expensive LLVM passes
Optimization Tips
Query Optimization
- Cache expensive computations
- Minimize query parameters
- Use shallow queries when possible
- Avoid unnecessary query dependencies
Memory Optimization
- Use arena allocation
- Intern strings and types
- Free temporary data
- Use compact data structures
Compilation Speed
- Enable incremental compilation
- Use query caching effectively
- Parallelize independent work
- Reduce redundant computation
Code Generation
- Optimize MIR before codegen
- Use appropriate optimization levels
- Enable LTO for release builds
- Profile-guided optimization
Performance Testing in CI
Setting Up Performance Tests
Resources
Profiling Guide
Rustc dev guide profiling chapter
measureme
Self-profiling framework
rustc-perf
Compiler performance benchmarking
Performance Book
Rust performance book
Next Steps
Debugging
Learn debugging techniques
Compiler Development
Return to compiler development
Library Development
Optimize library code