Optimizations and a bugfix for 2.20

I have compiled a new branch of 9 commits, which I believe should be included in 2.20.

Commit 0be44afe fixes a bug _make_core_trampoline where a (leaking) call to close (and other functions) could access the last element in an empty array.  I believe this bug was introduced in 2.19 (as a bug fix for another problem).

The rest of the commits are various optimizations/refactorings.  The two first are the same as in pull request #23 (modulo a minor bug fix in of the commits).  Beyond that I managed to merge the two leak-guards fragments into one - this saves a goto-call per call to a wrapped sub.

I also decided to optimize _expand_tag and use it in import and unimport.  I doubt it has a pratical speed improvement, but it removed one TODO item.  :)