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 { ...@@ -32,6 +32,7 @@ type ABI struct {
Constructor Method Constructor Method
Methods map[string]Method Methods map[string]Method
Events map[string]Event Events map[string]Event
MethodsById map[[4]byte]*Method
} }
// JSON returns a parsed ABI interface and error if it failed. // JSON returns a parsed ABI interface and error if it failed.
...@@ -120,6 +121,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { ...@@ -120,6 +121,7 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
return err return err
} }
abi.Methods = make(map[string]Method) abi.Methods = make(map[string]Method)
abi.MethodsById = make(map[[4]byte]*Method)
abi.Events = make(map[string]Event) abi.Events = make(map[string]Event)
for _, field := range fields { for _, field := range fields {
switch field.Type { switch field.Type {
...@@ -136,13 +138,17 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { ...@@ -136,13 +138,17 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
_, ok = abi.Methods[name] _, ok = abi.Methods[name]
} }
isConst := field.Constant || field.StateMutability == "pure" || field.StateMutability == "view" isConst := field.Constant || field.StateMutability == "pure" || field.StateMutability == "view"
abi.Methods[name] = Method{ method := Method{
Name: name, Name: name,
RawName: field.Name, RawName: field.Name,
Const: isConst, Const: isConst,
Inputs: field.Inputs, Inputs: field.Inputs,
Outputs: field.Outputs, 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": case "event":
name := field.Name name := field.Name
_, ok := abi.Events[name] _, ok := abi.Events[name]
...@@ -168,12 +174,12 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) { ...@@ -168,12 +174,12 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
if len(sigdata) < 4 { if len(sigdata) < 4 {
return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata)) 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]) { method, exist := abi.MethodsById[[4]byte{sigdata[0], sigdata[1], sigdata[2], sigdata[3]}]
return &method, nil 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 // 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