Wow. I used my own stack switch routine for several years now and didn't realize it could be improved until I read this thread. Thank you very much.
This is my old naive version:
https://github.com/ademakov/MainMemory/blob/master/src/base/...
This is what I have now:
https://github.com/ademakov/MainMemory/blob/cstack-switch-re...