accpnt

Notes on porting L-BFGS-B in Rust

09/06/2025

TAGS: rust, maths, algorithm

Initially started from a c2rust port of L-BFGS-B-C, this project aims to provide a pure and safe Rust implementation of the L-BFGS-B algorithm. Existing Rust crates are, for now, mostly wrappers around Fortran and C implementations. The purpose of this exercise is primarily educational but aims towards a modern take on this algorithm by removing Fortran or C specific idioms (such as goto and 1-based indexing).

This algorithm is widely used for solving bound-constrained optimization problems with memory efficiency in mind, notably by approximating the inverse Hessian matrix for second order evaluation of the objective function's curvature. Starting with an initial estimate of the optimal value that minimizes the objective function, this algorithm proceeds iteratively to refine it through differential calculus, while using limited computer memory.

Roughly, this algorithm minimizes a function f(x) where x is is bounded with a lower and upper boundary.

The porting process took around two months in total and made extensive use of LLMs (mostly Claude 3.5 Sonnet). I initially thought that using these "AI" tools would massively speed up the process, and I was quite thrilled about using them. However, I soon realized some of their limitations, notably in the debugging process. Using unit tests to validate the port, debugging generated code proved difficult many times. First, the L-BFGS-B algorithm itself is quite complex, and most of the functions are too large to be easily translated from unsafe to safe Rust, considering the processing limits of the LLM. Prompting them proved to be time-consuming, and I think I could have been more efficient from the beginning by attempting simpler tasks, which I ended up doing in the end. Second, the generated code was often incorrect, filled with compilation errors (which was usually fine for me) and—more concerning—functional errors, which proved more difficult to solve.

Still, LLMs are definitely disruptive and have totally changed the way I write code. I already think that development tasks have shifted to something else since they appeared, with more focus on architecture and testing.