txscript: Optimize ContainsStakeOpCodes.

This converts the ContainsStakeOpCodes function to make use of the new
tokenizer instead of the far less efficient parseScript thereby
significantly optimizing the function.

The following is a before and after comparison of analyzing a large
script:

benchmark                       old ns/op    new ns/op    delta
------------------------------------------------------------------
BenchmarkContainsStakeOpCodes   134599       968          -99.28%

benchmark                       old allocs   new allocs   delta
------------------------------------------------------------------
BenchmarkContainsStakeOpCodes   1            0            -100.00%

benchmark                       old bytes    new bytes    delta
------------------------------------------------------------------
BenchmarkContainsStakeOpCodes   466944       0            -100.00%
This commit is contained in:
Dave Collins 2019-03-13 01:12:06 -05:00
parent bfab5dbb93
commit 0e021a9564
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2

View File

@ -753,19 +753,20 @@ func getStakeOutSubscript(pkScript []byte) []byte {
// ContainsStakeOpCodes returns whether or not a pkScript contains stake tagging
// OP codes.
//
// NOTE: This function is only valid for version 0 scripts. Since the function
// does not accept a script version, the results are undefined for other script
// versions.
func ContainsStakeOpCodes(pkScript []byte) (bool, error) {
shPops, err := parseScript(pkScript)
if err != nil {
return false, err
}
for _, pop := range shPops {
if isStakeOpcode(pop.opcode.value) {
const scriptVersion = 0
tokenizer := MakeScriptTokenizer(scriptVersion, pkScript)
for tokenizer.Next() {
if isStakeOpcode(tokenizer.Opcode()) {
return true, nil
}
}
return false, nil
return false, tokenizer.Err()
}
// CalcScriptInfo returns a structure providing data about the provided script