core: cache code size retrievals to avoid db trashing

This commit is contained in:
Péter Szilágyi 2016-09-23 09:34:42 +03:00
parent 3c26ba4d30
commit ad16f46cfb
4 changed files with 22 additions and 3 deletions

View file

@ -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
}

View file

@ -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 {

View file

@ -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)

View file

@ -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)
}