I put the pure math codes in C extension of PHP. Building C extension for PHP is easier than most of the other high level languages. And then things get blazingly fast.
check out how a modern language deals with this stuff https://bun.sh/docs/api/ffi#usage