Port `state.py` to C or C++ for performance
Port
state.pyto C or C++ for performance