Speeding up texture sorting in SpriteBatcher.DrawBatch to draw thousands of textures in a frame

I was stress testing my game and ran into some performance issues. One of the places that was taking a couple milliseconds was sorting the textures by depth (SpriteBatcker.DrawBatch Array.Sort near the top). I popped in my own sort in place and was able to get a ~%15 in Release mode and ~%40 in Debug Mode speed boost that allows me to hit my frames on time.

You shouldn’t take my word for it so here’s a mocked up test you can paste into a console project and run.

If there’s interest I would also be happy to add this to the monogame source.