Skip to content

Compare the bindings

Every module exports compute_fibonacci(n). The workshop counts from compute_fibonacci(0) == 1 and compute_fibonacci(1) == 2. Index 44 gives 1,836,311,903, the last term that fits in a signed 32-bit result. Each implementation checks the index before entering the loop. Use Python integers from 0 through 44 for comparisons. Argument conversion is part of each binding: for example, Cython converts 1.5 to the C integer 1, while the other examples reject that float. Very large integers can also fail conversion before reaching the range check.

Example How the call reaches native code What to look for
CFFI Python declares a C ABI and opens a shared library at runtime. The declaration must agree with the compiled header.
Cython wrapping C A declaration tells Cython how to call the existing C function. The generated extension links against the C implementation.
Typed Cython Cython compiles the loop itself. C integer locals move arithmetic out of the Python interpreter.
SWIG An interface file tells SWIG which declarations to wrap. A generated Python proxy presents C functions and struct fields.
pybind11 A C++ module registers a function with pybind11. Its argument and return types determine how values are converted.
nanobind A second C++ module registers the same C++ function with nanobind. Compare the two registration calls while keeping the native work fixed.
PyO3 A Rust function is exported through a PyO3 module and built with maturin. Rust can return a Python exception through a PyResult.

Inspect the C interface

>>> from native_examples import cffi_fib, cython_fib, cython_typed, swig_fib
>>> [m.compute_fibonacci(5) for m in (cffi_fib, cython_fib, cython_typed, swig_fib)]
[13, 13, 13, 13]
>>> message = swig_fib.populate_struct(7, 42, 1024)
>>> message.type, message.job, message.size
(7, 42, 1024)

The CFFI example is useful when a library already exposes a C ABI and can ship as a shared object. Cython can call an existing C function or compile the hot loop itself. SWIG is useful when you have headers to expose and want the generator to produce wrappers. Compare the CFFI modes, Cython language guide, and SWIG Python guide.

Inspect the C++ and Rust interfaces

Read the C++ module definitions side by side in native/bindings/. Both call the implementation in native/core/. pybind11 offers an extensive set of conversions and integrations. nanobind was designed with smaller extension binaries and lower binding overhead in mind. The pybind11 build guide and nanobind design notes explain their tradeoffs.

For Rust, start at rust/src/lib.rs. Follow the Python argument into the native loop and the result back into Python. Cargo resolves Rust dependencies, PyO3 handles Python objects and exceptions, and maturin builds an installable extension. See the PyO3 guide and maturin guide.

PyPy is an alternative Python interpreter. This workshop tests CPython so that changes in interpreter and changes in binding library can be studied separately. The CFFI documentation also covers use with PyPy.