Skip to content

Commit

Permalink
signer/core: improve performance of isPrimitiveTypeValid function (et…
Browse files Browse the repository at this point in the history
…hereum#30274) (ethereum#30277)

Precomputes valid primitive types into a map to use for validation, thus removing sprintf.
  • Loading branch information
llkhacquan authored and leeren committed Aug 16, 2024
1 parent 664f10f commit 93cbc6a
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions signer/core/apitypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,39 +843,35 @@ func (t Types) validate() error {
return nil
}

// Checks if the primitive value is valid
func isPrimitiveTypeValid(primitiveType string) bool {
if primitiveType == "address" ||
primitiveType == "address[]" ||
primitiveType == "bool" ||
primitiveType == "bool[]" ||
primitiveType == "string" ||
primitiveType == "string[]" ||
primitiveType == "bytes" ||
primitiveType == "bytes[]" ||
primitiveType == "int" ||
primitiveType == "int[]" ||
primitiveType == "uint" ||
primitiveType == "uint[]" {
return true
var validPrimitiveTypes = map[string]struct{}{}

// build the set of valid primitive types
func init() {
// Types those are trivially valid
for _, t := range []string{
"address", "address[]", "bool", "bool[]", "string", "string[]",
"bytes", "bytes[]", "int", "int[]", "uint", "uint[]",
} {
validPrimitiveTypes[t] = struct{}{}
}
// For 'bytesN', 'bytesN[]', we allow N from 1 to 32
for n := 1; n <= 32; n++ {
// e.g. 'bytes28' or 'bytes28[]'
if primitiveType == fmt.Sprintf("bytes%d", n) || primitiveType == fmt.Sprintf("bytes%d[]", n) {
return true
}
validPrimitiveTypes[fmt.Sprintf("bytes%d", n)] = struct{}{}
validPrimitiveTypes[fmt.Sprintf("bytes%d[]", n)] = struct{}{}
}
// For 'intN','intN[]' and 'uintN','uintN[]' we allow N in increments of 8, from 8 up to 256
for n := 8; n <= 256; n += 8 {
if primitiveType == fmt.Sprintf("int%d", n) || primitiveType == fmt.Sprintf("int%d[]", n) {
return true
}
if primitiveType == fmt.Sprintf("uint%d", n) || primitiveType == fmt.Sprintf("uint%d[]", n) {
return true
}
validPrimitiveTypes[fmt.Sprintf("int%d", n)] = struct{}{}
validPrimitiveTypes[fmt.Sprintf("int%d[]", n)] = struct{}{}
validPrimitiveTypes[fmt.Sprintf("uint%d", n)] = struct{}{}
validPrimitiveTypes[fmt.Sprintf("uint%d[]", n)] = struct{}{}
}
return false
}

// Checks if the primitive value is valid
func isPrimitiveTypeValid(primitiveType string) bool {
_, ok := validPrimitiveTypes[primitiveType]
return ok
}

// validate checks if the given domain is valid, i.e. contains at least
Expand Down

0 comments on commit 93cbc6a

Please sign in to comment.