mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
core: cache code size retrievals to avoid db trashing
This commit is contained in:
parent
3c26ba4d30
commit
ad16f46cfb
4 changed files with 22 additions and 3 deletions
|
|
@ -96,8 +96,7 @@ type Account struct {
|
|||
Root common.Hash // merkle root of the storage trie
|
||||
CodeHash []byte
|
||||
|
||||
// TODO(fjl): track code size here to get it into the cache
|
||||
// codeSize int
|
||||
codeSize *int
|
||||
}
|
||||
|
||||
// NewObject creates a state object.
|
||||
|
|
@ -269,9 +268,20 @@ func (self *StateObject) Code(db trie.Database) []byte {
|
|||
return code
|
||||
}
|
||||
|
||||
// CodeSize returns the size of the contract code associated with this object.
|
||||
func (self *StateObject) CodeSize(db trie.Database) int {
|
||||
if self.data.codeSize == nil {
|
||||
self.data.codeSize = new(int)
|
||||
*self.data.codeSize = len(self.Code(db))
|
||||
}
|
||||
return *self.data.codeSize
|
||||
}
|
||||
|
||||
func (self *StateObject) SetCode(code []byte) {
|
||||
self.code = code
|
||||
self.data.CodeHash = crypto.Keccak256(code)
|
||||
self.data.codeSize = new(int)
|
||||
*self.data.codeSize = len(code)
|
||||
self.dirty, self.dirtyCode = true, true
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -171,6 +171,14 @@ func (self *StateDB) GetCode(addr common.Address) []byte {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (self *StateDB) GetCodeSize(addr common.Address) int {
|
||||
stateObject := self.GetStateObject(addr)
|
||||
if stateObject != nil {
|
||||
return stateObject.CodeSize(self.db)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (self *StateDB) GetState(a common.Address, b common.Hash) common.Hash {
|
||||
stateObject := self.GetStateObject(a)
|
||||
if stateObject != nil {
|
||||
|
|
|
|||
|
|
@ -94,6 +94,7 @@ type Database interface {
|
|||
GetNonce(common.Address) uint64
|
||||
SetNonce(common.Address, uint64)
|
||||
|
||||
GetCodeSize(common.Address) int
|
||||
GetCode(common.Address) []byte
|
||||
SetCode(common.Address, []byte)
|
||||
|
||||
|
|
|
|||
|
|
@ -363,7 +363,7 @@ func opCalldataCopy(instr instruction, pc *uint64, env Environment, contract *Co
|
|||
|
||||
func opExtCodeSize(instr instruction, pc *uint64, env Environment, contract *Contract, memory *Memory, stack *Stack) {
|
||||
addr := common.BigToAddress(stack.pop())
|
||||
l := big.NewInt(int64(len(env.Db().GetCode(addr))))
|
||||
l := big.NewInt(int64(env.Db().GetCodeSize(addr)))
|
||||
stack.push(l)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue