Fix #1035: StackOverflowError on save

by performing the easy and cheap check first.
Merge pull request #1039 from lucaswerkmeister/fix1035

Fix #1035: StackOverflowError on save