How about adding an extra type check in the if block which triggers the optimisation. Everything else can then fall back to the slower version?