Staden io_lib updates (ref handling)

A mixed bag of updates from Staden Package io_lib from myself and Rob.

These are primarily issues with handling references, bug fixing slow downs and inefficiencies especially when running in multi-threaded mode.

If you feel like cherry picking then the cram_external_encode() addition is irrelevant as it is unused for now, but added simply to keep in sync with io_lib code base. Similarly the empty container code doesn't yet matter too much and can be delayed until we have consensus on a full EOF block implementation.