A Practical Guide from Fundamentals to Production Systems
Introduction: Why Cython, Why Now
- What You Are About to Learn
- Who This Book Is For
- How This Book Is Structured
- A Note on Versions
- The Philosophy of This Book
Chapter 1: The Performance Landscape: When Cython Matters
- Why Python Is Slow: A Brief Diagnosis
- The Performance Toolchain: Options and Trade-offs
- When to Reach for Cython
- How Cython Fits Into the Ecosystem
- Case Study: Diagnosing a Real Bottleneck
Chapter 2: Getting Started: Installation, Setup, and First Steps
- Installing Cython and Dependencies
- Your First Cython Program
- Build Systems: setup.py, pyproject.toml, and setuptools
- The Compilation Pipeline Explained
- Running and Importing Cython Modules
Chapter 3: Cython Syntax and Static Typing
- From Python to Cython: The Minimal Changes
- Declaring Types: Variables, Parameters, and Returns
- Typed vs Untyped Code: What the Compiler Does Differently
- Functions and Methods — cdef, cpdef, and def
- Compiler Directives — Controlling Behavior with Annotations
- Reading the Generated C Code
- Common Pitfalls with Static Typing
Chapter 4: Memoryviews — Fast Array Access Without NumPy Dependency
- What Are Memoryviews and Why They Matter
- Declaring and Using Memoryviews
- Ownership Models — Borrowed vs Owned Memory
- Multidimensional Arrays and Broadcasting
- Memoryviews vs NumPy Arrays vs Typed Memoryviews
- Performance Example — Matrix Multiplication
- Common Pitfalls with Memoryviews
Chapter 5: Loops, Data Structures, and Algorithmic Patterns
- Optimizing Python Loops with Cython
- Parallel Loops with prange
- Native C Data Structures in Cython
- Efficient String and Dictionary Operations
- Common Algorithmic Patterns — Sorting, Searching, Accumulation
Chapter 6: Extension Types — Object-Oriented Cython
- Defining Extension Types with cdef class
- Properties and Methods — cdef vs cpdef vs def
- Inheritance and Polymorphism in Cython
- Memory Management for Custom Objects
- When to Use Extension Types vs Python Classes
Chapter 7: Interfacing with C and C++ Libraries
- Declaring External C Functions with cdef extern
- Using pxd Files for Interface Definitions
- Calling C Libraries — Complete Workflow
- C++ Integration — Classes, Templates, and Namespaces
- ABI Compatibility and Binary Distribution Challenges
Chapter 8: The GIL, Parallelism, and Concurrency
- Understanding the GIL — What It Is and Why It Matters
- Releasing the GIL with nogil
- Thread Safety in Cython Code
- Multiprocessing vs Threading with Cython
- Practical Patterns for Parallel Computation
Chapter 9: Memory Management and Native Data Types
- Native C Data Types in Cython
- Manual Memory Allocation with malloc and free
- Stack vs Heap Allocation Strategies
- Buffer Protocol and Zero-Copy Access
- Common Memory Bugs and How to Avoid Them
Chapter 10: Exception Handling and Error Management
- Python Exceptions in Cython Code
- C Errors and Return Codes
- Crossing the Boundary — Converting Between Systems
- noexcept and Performance Implications
- Defensive Programming Patterns
Chapter 11: Profiling, Benchmarking, and Diagnosing Bottlenecks
- Profiling Python Code to Find Hotspots
- Cython-Specific Profiling Tools
- Designing Valid Benchmarks
- Interpreting Results and Avoiding Pitfalls
- Regression Testing for Performance
Chapter 12: Optimization Strategies — From Incremental to Radical
- The Optimization Ladder — A Systematic Approach
- Compiler Optimization Flags and Their Impact
- Code Restructuring for Cython Performance
- Advanced Techniques — Inlining, Bounds Checking, Wraparound
- When to Stop Optimizing
Chapter 13: Build Systems, Packaging, and Distribution
- Modern Python Packaging with Cython
- Building Wheels for Distribution
- Managing C Dependencies in Packages
- Cross-Compilation and Manylinux
- Publishing to PyPI — Complete Workflow
Chapter 14: Debugging, Testing, and Maintainability
- Debugging Cython Code — Tools and Techniques
- Testing Compiled Extensions
- Maintaining Readable Cython Code
- API Design Principles for Cython Modules
- Versioning and Breaking Changes
Chapter 15: Real-World Case Studies — Production Cython in Action
- Case Study — High-Frequency Data Processing Pipeline
- Case Study — Computational Geometry Library
- Case Study — Integrating a C++ Machine Learning Library
- Lessons from Production Deployments
Conclusion — The Cython Mindset
- The Principles of Effective Cython Use
- Looking Ahead — Cython in the Evolving Python Ecosystem
- Your Next Steps
- Final Thought