working cython implementation to optimize table