btcsuite / btcd

An alternative full node bitcoin implementation written in Go (golang)
https://github.com/btcsuite/btcd/blob/master/README.md
ISC License
6.11k stars 2.32k forks source link

[txscript] Sigcache Implementation Needs Optimization #575

Closed davecgh closed 8 years ago

davecgh commented 8 years ago

I was doing a bit of profiling for other reasons tonight and I noticed the recently added signature cache is taking way more CPU time than it should. The following profile was generated when doing an initial block download, so there would be very few hits of the signature cache, nevertheless, as I've highlighted in the image, the signature cache additions and lookups are actually taking more time than the signature verifications!

sigcachecpuprof

I'm fairly certain this is the result of using a struct as the map key, which, in my experience, typically results in poor performance and this case appears to be no exception. I suspect making the map key a smaller fixed size array and using a fast hashing function (does not need to be a cryptographic hash function) to hash just the signature, then using that as the key would make a huge difference. Note that doing this would obviously also require making the struct that currently acts as the key the value for the entry and then ensuring that any hits are actually matches (since collisions would now be possible). In practice, the number of collisions that would require additional checking will be minimal and the more expensive full check would barely ever have to run in the case of cache misses.

wallclockbuilder commented 8 years ago

Apparently map key hashing for types: int32, int64 and string is superfast due to a special case fast path designed into the go runtime. Other types are condemned to slow hash times.

https://github.com/golang/go/issues/13271