Skip to content

Commit

Permalink
txscript: Optimize typeOfScript witness-pubkey-hash
Browse files Browse the repository at this point in the history
This continues 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 pubkey hash scripts
to use raw script analysis and the new tokenizer.

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

benchmark                          old ns/op     new ns/op     delta
BenchmarkIsWitnessPubKeyHash-8     61688         62839         +1.87%

benchmark                          old allocs     new allocs     delta
BenchmarkIsWitnessPubKeyHash-8     1              1              +0.00%

benchmark                          old bytes     new bytes     delta
BenchmarkIsWitnessPubKeyHash-8     311299        311299        +0.00%
  • Loading branch information
cfromknecht committed Feb 5, 2021
1 parent ddffa50 commit 7a471e0
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions txscript/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func extractWitnessPubKeyHash(script []byte) []byte {

return script[2:22]
}

return nil
}

Expand Down Expand Up @@ -430,6 +431,8 @@ func typeOfScript(scriptVersion uint16, script []byte) ScriptClass {
return PubKeyHashTy
case isScriptHashScript(script):
return ScriptHashTy
case isWitnessPubKeyHashScript(script):
return WitnessV0PubKeyHashTy
case isMultisigScript(scriptVersion, script):
return MultiSigTy
case isNullDataScript(scriptVersion, script):
Expand All @@ -441,9 +444,7 @@ func typeOfScript(scriptVersion uint16, script []byte) ScriptClass {
return NonStandardTy
}

if isWitnessPubKeyHash(pops) {
return WitnessV0PubKeyHashTy
} else if isWitnessScriptHash(pops) {
if isWitnessScriptHash(pops) {
return WitnessV0ScriptHashTy
}

Expand Down

0 comments on commit 7a471e0

Please sign in to comment.