Optimise to_primitive common cases

to_primitive included many checks that were executed before any usual
types have been checked. It's safe to reorder / duplicate the most
common ones. Especially built-in types can be checked before other
more complicated scenarios. This is important since to_primitive gets
called over a million times in usual test run and many more times in a
live environment.

This change strips around 4% of nova testing time on my machine and the
function itself is ~5x faster on average according to the profiler.

Change-Id: I71e0c06bbcc31793a1cdcebb36d4e3d8c5876f73