[retarget #1994 to 2.10.x] SI-6726 Improving pattern matcher analysis performance

A dash of hash consing and a pinch of loop fusion.

These were just follow-the-profiler changes; I suspect there are bigger wins to be had. Perhaps the wins here don't merit closing the ticket.

Perhaps we could think about how to efficiently handle the common case where each case performs a single a) type test against a leaf type (one not subclassed by any other types that are tested), or b) equality test against a constant that is also unique within the match.

I'll leave consideration of that the reviewer, @adriaanm.