Performance improvements for astropy.units

- For compose, the set of destination units are prefiltered to only
  include units that have bases in common with "self".  This cuts down
  on the search space considerably in most cases.

- Inline some delegation functions.

- Replace 2 isinstance calls with 1

- Cache the hash of composite units, which can be expensive to compute.

This is partially in response to #2479, though it probably makes sense to also include #2542 as well (though this and #2542 are completely independent).