Performance improvements for DiffCount/diff --count

Fixes #741. See the commit message of 99b42c30e91db7b5d48410f6b4d9b385849eadc9 for more info.