massive speedup by moving id tracking to c++, fixed memory corruption crash