Merge pull request #850 from testn/topological-perf

Performance improvement for topological sort