Fix n² complexity in cfkey

The optimizer has a helper that checks statements for equality, cfkey.  It is currently n², and could be made linear by memoization.
Both cfkey_cache and cfkey should be local.  Once this is fixed, LGTM to land.