Resolve issues 4366 and 4420 for better patch performance

I rebased the last two commits here on another change in my PR for branch 'test'. If you take this PR no need for that one. The test changes were necessary but otherwise unrelated, so they were a separate commit.
Perhaps an older version did, because that was the experience sometime back. Or maybe it was when I used a different base class. We're talking O(1) here, so I'm not that concerned unless you want me to submit a post-merge commit.