Assorted optimizations

This incidentally fixes #4240, but the main purpose is to speed up any "low hanging fruit" I could find via kcachegrind results on relap-7.