Show HN: Copy-and-patch compiler for hard real-time Python (github.com)
I built Copapy as an experiment: Can Python be used for hard real-time systems?
Instead of an interpreter or JIT, Copapy builds a computation graph by tracing Python code and uses a custom copy-and-patch compiler. The result is very fast native code with no GC, no syscalls, and no memory allocations at runtime.
The copy-and-patch compiler currently supports x86_64 as well as 32- and 64-bit ARM. It comes as small Python package with no other dependencies - no cross-compiler, nothing except Python.
The current focus is on robotics and control systems in general. This project is early but already usable and easy to try out.
Would love your feedback!
10 comments
[ 3.0 ms ] story [ 33.5 ms ] threadI always wondered if this could be possible. Like you fuzz a program, map out each possible allocation and deallocation and optimize the code with those hot paths and some statistics.
Very interesting project, would love some sort of write up on it.
However, from my experience this programming style is ok for simple state machines, but it’s definitely not great, and if things get more complex, it’s getting really hard to keep it comprehensible and correct.
I think the main challenge is the design of an API that fits state machines. Concerning the WCET, branchless code should be on average not worse than branched code.
I guess, that's only achievable for certain kind of code, already designed with hard real-time in mind. It would be good to have some information about the limitations of this approach.
It's fine that it is still in development but it just seems worth being upfront.