Documentation Index Fetch the complete documentation index at: https://mintlify.com/rust-lang/rust/llms.txt
Use this file to discover all available pages before exploring further.
This guide covers profiling the Rust compiler to understand and improve its performance, including compilation speed, memory usage, and runtime efficiency.
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
Build for profiling
Configure config.toml for profiling: [ rust ]
# Enable profiler runtime
profiler = true
# Debug info for profiling
debuginfo-level = 1
debuginfo-level-rustc = 1
# Optimize for profiling
optimize = true
# Keep frame pointers for better stack traces
frame-pointers = true
Build the compiler
# Build optimized compiler with profiling support
./x.py build --stage 1
# Or build specific components
./x.py build compiler/rustc_driver
Install profiling tools
# perf
sudo apt-get install linux-tools-common linux-tools-generic
# flamegraph
cargo install flamegraph
# measureme
cargo install measureme
# samply
cargo install samply
# Instruments (Xcode)
xcode-select --install
# cargo-instruments
cargo install cargo-instruments
# measureme
cargo install measureme
# samply
cargo install samply
# measureme
cargo install measureme
# Windows Performance Analyzer
# Download from Microsoft
# samply
cargo install samply
Self-Profiling with measureme
Rustc has built-in self-profiling using the measureme framework:
Basic Self-Profiling
Enable Self-Profiling
Profile Events
Analyze Results
# Profile a compilation
rustc -Z self-profile file.rs
# This creates file-pid.mm_profdata
# Profile with custom name
rustc -Z self-profile=my_profile file.rs
# Profile specific events
rustc -Z self-profile-events=default,args file.rs
# Available event types:
# - default: basic profiling
# - query-cache-hits: query cache statistics
# - query-blocked: query blocking information
# - incr-result-hashing: incremental hash computation
# - args: function arguments
# Install analysis tools
cargo install measureme
# Summarize results
summarize file-pid.mm_profdata
# Generate flamegraph
flamegraph file-pid.mm_profdata
# Generate Chrome trace
crox file-pid.mm_profdata
# Open chrome://tracing and load file.json
Analyzing Self-Profile Data
Summary
Flamegraph
Chrome Tracing
Diff
# Get summary statistics
summarize file-pid.mm_profdata
# Output shows:
# - Total time
# - Time per query
# - Invocation counts
# - Cache hit rates
Time-Passes Profiling
Track time spent in each compiler pass:
Basic Time Tracking
With LLVM Timing
Pretty Printing
# Time all passes
rustc -Z time-passes file.rs
# Output shows time for each pass:
# - parsing
# - expansion
# - type checking
# - borrowck
# - codegen
# Include LLVM pass timing
rustc -Z time-passes -Z time-llvm-passes file.rs
# Shows detailed LLVM optimization times
# Pretty print timing info
rustc -Z time-passes file.rs 2>&1 | grep "time:"
# Save to file for analysis
rustc -Z time-passes file.rs 2>&1 | tee timing.txt
CPU Profiling
Using perf (Linux)
Record profile
# Record CPU profile
perf record -F 99 -g ./build/stage1/bin/rustc file.rs
# Record with call graph
perf record -F 99 --call-graph dwarf ./build/stage1/bin/rustc file.rs
Analyze results
# Interactive report
perf report
# Show call graph
perf report --stdio
# Filter by function
perf report --stdio | grep function_name
# Generate flamegraph from perf data
perf script | stackcollapse-perf.pl | flamegraph.pl > flame.svg
# Or use cargo-flamegraph
cargo flamegraph --bin rustc -- file.rs
Using Instruments (macOS)
Time Profiler
Allocations
System Trace
# Profile with Time Profiler
cargo instruments --template time --bin rustc -- file.rs
# Opens Instruments.app with results
Basic Profiling
With Arguments
# Profile with samply
samply record ./build/stage1/bin/rustc file.rs
# Opens profiler.firefox.com with results
# Profile with rustc arguments
samply record ./build/stage1/bin/rustc -C opt-level= 3 file.rs
Memory Profiling
Heap Profiling with Valgrind
# Profile heap usage over time
valgrind --tool=massif ./build/stage1/bin/rustc file.rs
# Analyze with ms_print
ms_print massif.out.12345
# Visualize with massif-visualizer
massif-visualizer massif.out.12345
# Detailed heap analyzer
valgrind --tool=dhat ./build/stage1/bin/rustc file.rs
# Shows:
# - Allocation patterns
# - Peak memory usage
# - Lifetime of allocations
Memory Statistics
Print Type Sizes
Memory Usage
Allocation Tracking
# Show size of types in compilation
rustc -Z print-type-sizes file.rs
# Helps identify large types causing memory issues
Query Profiling
The compiler’s query system is a major performance factor:
Query Statistics
Dependency Graph
Query Blocking
# Profile query execution
rustc -Z self-profile-events=default,query-cache-hits file.rs
# Analyze cache hit rates
summarize file-pid.mm_profdata --query-stats
# Dump query dependency graph
rustc -Z dump-dep-graph file.rs
# Creates dep-graph.txt showing query dependencies
# Track query blocking
rustc -Z self-profile-events=query-blocked file.rs
# Shows which queries block on others
Incremental Compilation
Incremental Stats
Incremental Profiling
# Enable incremental compilation stats
RUSTC_LOG = rustc_incremental = debug rustc file.rs
# Shows:
# - Cache hits/misses
# - Reused query results
# - Invalidated queries
LLVM Pass Timing
Time LLVM Passes
LLVM Stats
# Time all LLVM passes
rustc -Z time-llvm-passes file.rs
# With detailed output
rustc -C llvm-args=-time-passes file.rs
# Enable LLVM statistics
rustc -C llvm-args=-stats file.rs
# Shows:
# - Number of functions
# - Instructions processed
# - Pass execution counts
LLVM Profiling
PGO (Profile-Guided Optimization)
LLVM Remarks
# Step 1: Build with instrumentation
rustc -C profile-generate=./pgo-data file.rs
# Step 2: Run the program
./file
# Step 3: Process profile data
llvm-profdata merge -o merged.profdata ./pgo-data
# Step 4: Build with profile
rustc -C profile-use=merged.profdata file.rs
Benchmarking
Compiler Benchmarks
Run rustc-perf
# Clone rustc-perf
git clone https://github.com/rust-lang/rustc-perf
cd rustc-perf
# Build collector
cargo build --release -p collector
# Run benchmarks
./target/release/collector bench_local ./build/stage1/bin/rustc
Analyze results
# View results
./target/release/collector analyze
# Compare with baseline
./target/release/collector compare baseline.json new.json
Custom Benchmarks
# Install hyperfine
cargo install hyperfine
# Benchmark compilation
hyperfine './build/stage1/bin/rustc file.rs'
# Compare different versions
hyperfine \
--warmup 3 \
'./old-rustc file.rs' \
'./new-rustc file.rs'
Establish baseline
# Profile before changes
rustc -Z self-profile=baseline file.rs
summarize baseline-pid.mm_profdata > baseline.txt
Make changes
Implement your performance improvement
Profile again
# Profile after changes
rustc -Z self-profile=improved file.rs
summarize improved-pid.mm_profdata > improved.txt
Compare results
# Compare profiles
mmview diff baseline-pid.mm_profdata improved-pid.mm_profdata
# Look for:
# - Reduced query execution time
# - Better cache hit rates
# - Fewer invocations
Validate improvement
# Run benchmarks
./target/release/collector bench_local ./build/stage1/bin/rustc
# Check against perf suite
./x.py test src/tools/rustc-perf
Symptoms: High time in specific queriesDiagnosis: rustc -Z self-profile file.rs
summarize file-pid.mm_profdata --query-stats
Solutions:
Add caching
Optimize hot paths
Reduce query invocations
Symptoms: Low query cache hitsDiagnosis: rustc -Z self-profile-events=query-cache-hits file.rs
Solutions:
Improve query key design
Enable incremental compilation
Reduce query invalidation
Symptoms: Excessive memory consumptionDiagnosis: valgrind --tool=massif ./build/stage1/bin/rustc file.rs
Solutions:
Reduce type sizes
Optimize data structures
Free unused memory
Symptoms: Long time in codegen phaseDiagnosis: rustc -Z time-llvm-passes file.rs
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
GitHub Actions
Regression Check
name : Performance Test
on : [ pull_request ]
jobs :
perf :
runs-on : ubuntu-latest
steps :
- uses : actions/checkout@v2
- name : Build
run : ./x.py build --stage 1
- name : Profile
run : |
rustc -Z self-profile test.rs
summarize test-*.mm_profdata > results.txt
- name : Check for regressions
run : ./check-perf-regression.sh
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