SPy (Static Python) lang: fast as C, Pythonic as Python — Antonio Cuni

Learn about SPy, a new Python variant designed to match C's speed while keeping Python's syntax. Explores static typing, compilation targets, and performance optimization approach.

Key takeaways
  • SPy (Static Python) is a new language variant/compiler that aims to be as fast as C while maintaining Python-like syntax and features

  • Key differences from Python include:

    • Formalized execution phases (import time vs runtime)
    • Static type checking and enforcement
    • Restricted dynamism for better compilation
    • Type annotations are actually enforced
  • Performance improvements achieved through:

    • Partial evaluation (“redshifting”) to optimize code at import time
    • Static type-based operation dispatch
    • Compilation to native code via C
    • Removal of dynamic lookup overhead
  • Multiple compilation targets supported:

    • Native code (via C)
    • WebAssembly (via Emscripten)
    • JavaScript interop
  • Core design principles:

    • No “performance cliffs” - predictable optimization
    • Maintain Pythonic feel and features where possible
    • Simple implementation and mental model
    • Focus on common Python usage patterns
  • Current status:

    • Work in progress/prototype stage
    • Basic features working including functions, classes, types
    • Missing some Python features like lists/dicts
    • Open source on GitHub (spylang/spy)
  • Use cases include:

    • Numerical computation
    • Performance-critical modules
    • WebAssembly compilation
    • Alternative to Cython for extensions
  • Not meant to be 100% Python compatible - intentionally restricts some dynamic features to enable better optimization