🤖 AI Summary
A developer experimented with translating a small, Cython-accelerated inner loop from scikit-learn’s DBSCAN (dbscan_inner) into Mojo using the new Mojo→Python interop (beta in Max 25.4). The translation was syntactically straightforward—most of the logic copied over—and required some Mojo boilerplate (PythonModuleBuilder export) and replacing C++ vector with Mojo List[Int]. All DBSCAN tests passed, showing functional parity. The interop currently supports only Mojo functions taking up to three PythonObject arguments, which shaped the choice of this example.
Performance initially suffered because the Mojo version operated on opaque PythonObject values (≈0.0227s vs Cython ≈2.78e-05s, ~800× slower). Converting numpy inputs to Mojo Spans via ctypes.data.unsafe_get_as_pointer[DType]() removed copies and exposed typed memory to the Mojo compiler, bringing runtime to ~8.59e-05s (about 3× slower than Cython). However, translating this inner loop didn’t change end-to-end DBSCAN runtime since radius-neighbors computation dominates (≈95% of time). Key takeaways: Mojo shows promise as a Cython replacement for hot loops if numpy/array types are mapped to Mojo-native views (Spans); PythonObject overhead is the bottleneck; handling lists of variable-length arrays (neighborhoods) is still tricky. The author suggests best-practice docs for mapping Python/numpy to Mojo and plans to target heavier, parallelizable parts (possibly GPU) next.
Loading comments...
login to comment
loading comments...
no comments yet