Commit b290cfe0 authored by Mark Tyneway's avatar Mark Tyneway Committed by GitHub

optimization: add back in the abi caching (#492)

* optimization: add back in the geohot abi caching

* changeset: patch
parent c4266faf
---
"@eth-optimism/l2geth": patch
---
CPU Optimization by caching ABI methods
......@@ -32,6 +32,7 @@ type ABI struct {
Constructor Method
Methods map[string]Method
Events map[string]Event
MethodsById map[[4]byte]*Method
}
// JSON returns a parsed ABI interface and error if it failed.
......@@ -120,6 +121,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
return err
}
abi.Methods = make(map[string]Method)
abi.MethodsById = make(map[[4]byte]*Method)
abi.Events = make(map[string]Event)
for _, field := range fields {
switch field.Type {
......@@ -136,13 +138,17 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
_, ok = abi.Methods[name]
}
isConst := field.Constant || field.StateMutability == "pure" || field.StateMutability == "view"
abi.Methods[name] = Method{
method := Method{
Name: name,
RawName: field.Name,
Const: isConst,
Inputs: field.Inputs,
Outputs: field.Outputs,
}
abi.Methods[name] = method
// add method to the id cache
sigdata := method.ID()
abi.MethodsById[[4]byte{sigdata[0], sigdata[1], sigdata[2], sigdata[3]}] = &method
case "event":
name := field.Name
_, ok := abi.Events[name]
......@@ -168,12 +174,12 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
if len(sigdata) < 4 {
return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata))
}
for _, method := range abi.Methods {
if bytes.Equal(method.ID(), sigdata[:4]) {
return &method, nil
}
method, exist := abi.MethodsById[[4]byte{sigdata[0], sigdata[1], sigdata[2], sigdata[3]}]
if !exist {
return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
}
return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
return method, nil
}
// EventByID looks an event up by its topic hash in the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment