Performance and stability improvements to wcs.all_world2pix

Rebased and squashed @mcara's changes from #2373.