Optimize goal changed

Make goal changed approximately O(paths + targets), instead of something much worse.

Testing Done:
Correctness: https://travis-ci.org/pantsbuild/pants/builds/43764453

Performance: tested on a megachange branch on Twitter's monorepo; previous time was on the order of hours; time with patch is 3 minutes.

Reviewed at https://rbcommons.com/s/twitter/r/1470/
Sync of https://github.com/pantsbuild/pants/commit/2dd17a90d0c6d994d517f1aba21a30da2fd3d3fd