You have a Python codebase. Thousands of lines. It works. Your team knows it. Your tests cover it. Your CI/CD deploys it. But there’s this one function. It shows up in every profile. It’s eating 80% of your runtime. You know it would be faster in C++. The traditional answer? Rewrite it in C++, then spend sometime writing pybind11 boilerplate to call it from Python. Or just… don’t bother. Mirror Bridge is a third option: write C++, run one command, done. The Setup Let’s say you have a simple operation - a dot product: // vec3.hpp struct Vec3 { double x, y, z; Vec3(double x, double y, double z) : x(x), y(y), z(z) {} double dot(const Vec3& other) const { return x * other.x + y * other.y + z * other.z; } // Compute the magnitude (norm) of the vector double length() const { return std::sqrt(x*x + y*y + z*z); } }; To use this from Python: ./mirror_bridge_auto src/ --module vec3 -o . That’s it. No binding code. Mirror Bridge uses C++26 reflection to discover your classes, methods, and fields automatically. import vec3 a = vec3.Vec3(1, 2, 3) b = vec3.Vec3(4, 5, 6) print(a.dot(b)) # 32.0 The Naive Benchmark (And Why It’s Misleading) Let’s call dot() a million times: for _ in range(1_000_000): a.dot(b) Results (M3 Max MacBook Pro): Python class: 0.11s C++ via Mirror Bridge: 0.04s Speedup: 2.9x A ~3x speedup. Not bad, but not eye-popping. What’s going on? This benchmark can be somewhat misleading Each call from Python to C++ pays a toll - argument conversion, language boundary crossing, result conversion. For a trivial operation like dot() (3 multiplies, 2 adds), that toll dominates the actual work. This is like benchmarking a Ferrari by measuring how fast it can start and stop at every intersection. You’re measuring overhead, not speed. Dissecting the Cross-Language Barrier What actually happens when Python calls a C++ function? Let’s trace through a single a.dot(b) call: Method lookup. Python sees a.dot and searches for the dot attribute. It checks a.__dict__, then type(a)._...
First seen: 2025-12-04 07:06
Last seen: 2025-12-07 03:21