topsort caching