This was a great read, thanks. OP, readers might benefit from having it explicitly mentioned that while the pseudocode is in Python, actual Python code will likely not benefit from such an approach because of how memory is fragmented in the standard Python implementation - e.g. this discussion: https://stackoverflow.com/questions/49786441/python-get-proc...
I am tempted to actually test this (i.e. see if there's a speedup in Python), but I don't have the time right now.