Skip to content
This repository was archived by the owner on Dec 30, 2025. It is now read-only.

programmersd21/adrenaline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ Adrenaline: Python β†’ Rust β†’ Native Compiler

A production-grade compiler that transforms slow Python code into blazingly fast native executables. Achieve 10–1000Γ— speedups automatically with zero annotations.

🎯 Core Features

1. Intelligent Compilation Pipeline

Python β†’ AST β†’ Type Inference β†’ IR β†’ Optimization β†’ Rust β†’ Native Binary

2. Compiler Directives (Optional)

Guide compilation with simple Python comments:

def hot_function():
    """
    #adrenaline:hot           # Aggressive optimization
    #adrenaline:simd          # Auto-vectorization
    #adrenaline:parallel      # Multi-threading with Rayon
    #adrenaline:inline        # Inline at call sites
    #adrenaline:no-compile    # Fall back to Python
    #adrenaline:cache         # Cache compiled output
    """
    # Your code here
    pass

3. Automatic Intelligence

  • Type Inference: Automatically infer int, float, array types
  • Hot Path Detection: Profile and recompile hot functions at higher optimization levels
  • SIMD Detection: Identify numeric loops suitable for vectorization
  • Parallelization: Safe auto-threading of independent loops
  • Fallback Execution: Unsupported Python features automatically fall back to CPython

4. Real Optimizations

  • Loop unrolling
  • Constant folding
  • Dead code elimination
  • Bounds check elimination (proven-safe accesses)
  • Common subexpression elimination
  • SIMD vectorization
  • Parallel execution (Rayon)
  • Function inlining for hot paths
  • Memoization for pure functions

5. CLI Interface

adrenaline build main.py              # Compile Python to native
adrenaline run main.py arg1 arg2      # Execute compiled binary
adrenaline check main.py              # Check for compilation issues
adrenaline cache clear                # Clear compilation cache
adrenaline help directives            # Show directive syntax
adrenaline help features              # Show supported features

πŸ“¦ Installation

Prerequisites

  • Rust 1.70+ with rustc
  • Python 3.10+
  • Cargo

Build from Source

Windows (PowerShell):

git clone https://github.com/yourusername/adrenaline.git
cd adrenaline
.\build.ps1

Linux/macOS:

git clone https://github.com/yourusername/adrenaline.git
cd adrenaline
chmod +x build.sh
./build.sh

The compiled binary will be placed in dist/adrenaline (or dist/adrenaline.exe on Windows).

πŸš€ Quick Start

Example 1: Simple Loop

# examples/basic.py
def sum_range(n):
    total = 0
    for i in range(n):
        total += i
    return total

if __name__ == "__main__":
    print(sum_range(1000000))

Compile and run:

adrenaline build examples/basic.py
adrenaline run examples/basic.py

Example 2: Hot Functions

# examples/directives.py
def hot_compute(iterations):
    """
    #adrenaline:hot
    #adrenaline:simd
    Intensive computation - marked for aggressive optimization
    """
    result = 0
    for i in range(iterations):
        result += (i * i) % 97
    return result

if __name__ == "__main__":
    print(hot_compute(10000000))

Example 3: Fallback for Unsupported Features

# examples/fallback.py
def use_dict():
    """
    #adrenaline:no-compile
    Dictionary operations aren't yet supported - fallback to Python
    """
    data = {"key": "value"}
    return data.get("key")

def regular_function(x):
    # This will be compiled
    return x * x + 2 * x + 1

if __name__ == "__main__":
    print(use_dict())
    print(regular_function(42))

πŸ“Š Supported Python Features

βœ… Fully Supported

  • Function definitions and calls
  • Local and global variables
  • Numeric types: int, float
  • Lists and arrays
  • For/while loops
  • If/elif/else conditionals
  • Binary and unary operators (+, -, *, /, //, %, **, &, |, ^, <<, >>)
  • Comparison operators (==, !=, <, <=, >, >=)
  • Type inference
  • Local imports

⏳ Planned Support

  • Classes and OOP
  • Generators and iterators
  • Decorators (beyond directives)
  • Global state management
  • Dictionary/set operations
  • String manipulation (partial)

❌ Unsupported (Use #adrenaline:no-compile)

  • Advanced Python features (metaclasses, descriptors)
  • Dynamic code generation
  • Complex context managers

πŸ”§ Architecture

src/
β”œβ”€β”€ main.rs              # CLI entry point
β”œβ”€β”€ cli.rs               # Command-line interface (clap)
β”œβ”€β”€ parser.rs            # Python source parsing
β”œβ”€β”€ ast_types.rs         # AST type definitions
β”œβ”€β”€ type_inference.rs    # Type inference engine
β”œβ”€β”€ ir.rs                # Intermediate representation
β”œβ”€β”€ optimizer.rs         # IR optimization passes
β”œβ”€β”€ codegen.rs           # Rust code generation
β”œβ”€β”€ compiler.rs          # Main compilation pipeline
β”œβ”€β”€ directives.rs        # Compiler directive system
β”œβ”€β”€ profiler.rs          # Runtime profiling
β”œβ”€β”€ runtime.rs           # Runtime support
β”œβ”€β”€ cache.rs             # SHA256-based compilation cache
└── diagnostics.rs       # Error reporting (miette)

🎯 Optimization Levels

The compiler automatically applies different optimization strategies:

  • Basic (default): Constant folding, dead code elimination
  • Aggressive (hot functions): Loop unrolling, SIMD, bounds check elimination
  • Extreme (deeply profiled): All of the above + function inlining, escape analysis

πŸ“ˆ Performance Tips

  1. Profile First: Mark hot functions with #adrenaline:hot
  2. Use Numeric Types: Prefer int/float over generic collections
  3. Enable SIMD: Use #adrenaline:simd for vectorizable loops
  4. Parallelize: Use #adrenaline:parallel for independent iterations
  5. Cache Results: Enable #adrenaline:cache for expensive functions

πŸ”„ Compilation Cache

Adrenaline caches compiled outputs based on source code SHA256 hash. Clear cache with:

adrenaline cache clear

πŸ› οΈ Development

Build Debug Version

cargo build
./target/debug/adrenaline build examples/basic.py

Run Tests

cargo test

Check for Issues

cargo clippy
cargo fmt

πŸ“„ License

MIT / Apache-2.0 (dual licensed)

🀝 Contributing

Contributions welcome! Areas of focus:

  • Python AST parsing improvements
  • Additional optimization passes
  • More language feature support
  • Performance benchmarking

πŸ“š References


Made with ⚑ for speed-obsessed Python developers