Attempt to optimize the use of key compression

Key compression functions are now memoized.