[perf] Fetch keys of mempool history maps outside of loop.

I'm running an electrum server node on somewhat slow hardware and suffering a bit from high cpu usage,  This change cuts time spent in memory pool processing by more than half.
On my core i7 it goes from ~3.5 to ~1.5 seconds and on my slow node (atom cpu) from 60+ seconds to ~20 seconds.