Decentralize the algorithm caching code and use a map rather than an array to save memory.