txscript: Optimize typeOfScript for witness-script-hash

This concludes the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of witness script hash scripts
to use raw script analysis and the new tokenizer.

With all of the limbs now useing optimized variants, the following is a
before an after comparison of calling GetScriptClass on a large script:

benchmark                     old ns/op     new ns/op     delta
BenchmarkGetScriptClass-8     61515         15.3          -99.98%

benchmark                     old allocs     new allocs     delta
BenchmarkGetScriptClass-8     1              0              -100.00%

benchmark                     old bytes     new bytes     delta
BenchmarkGetScriptClass-8     311299        0             -100.00%
This commit is contained in:
Conner Fromknecht 2019-04-19 02:04:01 -07:00 committed by Roy Lee
parent 918278a251
commit 6e0abb61bd

View file

@ -441,22 +441,15 @@ func typeOfScript(scriptVersion uint16, script []byte) ScriptClass {
return ScriptHashTy return ScriptHashTy
case isWitnessPubKeyHashScript(script): case isWitnessPubKeyHashScript(script):
return WitnessV0PubKeyHashTy return WitnessV0PubKeyHashTy
case isWitnessScriptHashScript(script):
return WitnessV0ScriptHashTy
case isMultisigScript(scriptVersion, script): case isMultisigScript(scriptVersion, script):
return MultiSigTy return MultiSigTy
case isNullDataScript(scriptVersion, script): case isNullDataScript(scriptVersion, script):
return NullDataTy return NullDataTy
} default:
pops, err := parseScript(script)
if err != nil {
return NonStandardTy return NonStandardTy
} }
if isWitnessScriptHash(pops) {
return WitnessV0ScriptHashTy
}
return NonStandardTy
} }
// GetScriptClass returns the class of the script passed. // GetScriptClass returns the class of the script passed.