Overview
Shed Skin provides several mechanisms for optimizing program performance. This guide covers advanced optimization flags, manual optimization techniques, and profiling strategies.Optimization Flags
Basic Optimization
Compile with standard optimizations:-O2 -march=native
Advanced Compiler Flags
Customize optimization with a flags file:~/.shedskin/flags/optimize:
Profile-Guided Optimization (PGO)
PGO uses runtime profiling to guide optimization: Step 1: Compile with instrumentation~/.shedskin/flags/pgo-generate:
~/.shedskin/flags/pgo-use:
Understanding Generated C++ Code
Analyzing Generated Code
Examine the generated C++ to identify optimization opportunities:- Unnecessary type conversions
- Virtual method calls
- Memory allocations
- Repeated computations
Hot Path Optimization
Focus optimization on code executed most frequently:- Identify hot paths with profiling (see below)
- Rewrite hot functions in Python for better C++ generation
- Consider manual C++ for critical sections
Manual Optimization Techniques
List Comprehensions vs Loops
List comprehensions often generate more efficient code:Avoiding Dynamic Dispatch
Static types enable direct calls:Local Variables
Local variables are faster than globals or attributes:Integer vs Float
Integer arithmetic is faster than floating-point:String Building
Use join() for efficient string concatenation:Container Pre-allocation
Pre-allocate when final size is known:Memory Management Tuning
Garbage Collection Configuration
Tune the Boehm GC for your workload:Disabling GC
For programs with bounded memory usage:- Memory usage is bounded and known
- Program runs for short duration
- You’ve verified no leaks with testing
Object Pooling
Reuse objects instead of allocating:Profiling
CPU Profiling
Use standard profiling tools on generated executable: perf (Linux):Identifying Bottlenecks
Look for:- Hot functions - Functions consuming most CPU time
- Allocation patterns - Frequent memory allocations
- Cache misses - Poor memory access patterns
- Virtual calls - Polymorphic dispatch overhead
Python-Level Profiling
Profile before compilation to identify algorithmic issues:Benchmarking
Micro-benchmarks
Measure specific operation performance:Comparing Implementations
Automated Benchmarking
Advanced Tuning
Type Inference Limits
Adjust type inference parameters inshedskin/infer.py for large codebases:
- Higher limits: More precise types, slower compilation, potentially faster runtime
- Lower limits: Faster compilation, less precise types, potentially slower runtime
Compilation Time
For faster iteration during development:~/.shedskin/flags/debug:
Integer Size Selection
Choose appropriate integer size:Optimization Checklist
Before optimizing:- Profile to identify actual bottlenecks
- Verify correctness with tests
- Establish baseline performance metrics
- Use list comprehensions where appropriate
- Cache frequently accessed attributes in locals
- Pre-allocate containers when size is known
- Use join() for string building
- Prefer integer arithmetic when possible
- Minimize dynamic dispatch with type hints
- Use appropriate -O level (start with -O2)
- Enable -march=native for CPU-specific optimizations
- Consider PGO for critical applications
- Profile generated C++ and adjust Python code accordingly
- Tune GC parameters for workload
- Consider —nogc for bounded memory programs
- Use object pooling for frequently allocated types
- Monitor memory usage with valgrind
Expected Performance
Typical speedups over CPython:- Numeric code: 2-100x faster
- String processing: 2-50x faster
- General algorithms: 2-40x faster
- With optimization: Additional 10-30% improvement
Further Reading
- GCC optimization options: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
- Boehm GC tuning: https://www.hboehm.info/gc/
- CPU profiling guide: https://perf.wiki.kernel.org/index.php/Tutorial
- Generated code analysis:
shedskin/cpp.pysource