Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) cache codehash for performance #471

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion fuzzing/coverage/coverage_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ type CoverageTracer struct {

// nativeTracer is the underlying tracer used to capture EVM execution.
nativeTracer *chain.TestChainTracer

// codeHashCache is a cache for values returned by getContractCoverageMapHash,
// so that this expensive calculation doesn't need to be done every opcode.
// The Hash key is a contract's codehash (which uniquely identifies it),
// and the bool key is whether we're in contract init (true) or runtime (false)
// (since init vs runtime produces different results from getContractCoverageMapHash).
codeHashCache map[bool]map[common.Hash]common.Hash
}

// coverageTracerCallFrameState tracks state across call frames in the tracer.
Expand All @@ -71,6 +78,7 @@ func NewCoverageTracer() *CoverageTracer {
tracer := &CoverageTracer{
coverageMaps: NewCoverageMaps(),
callFrameStates: make([]*coverageTracerCallFrameState, 0),
codeHashCache: map[bool]map[common.Hash]common.Hash{false: make(map[common.Hash]common.Hash), true: make(map[common.Hash]common.Hash)},
}
nativeTracer := &tracers.Tracer{
Hooks: &tracing.Hooks{
Expand Down Expand Up @@ -159,11 +167,17 @@ func (t *CoverageTracer) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tr
scopeContext := scope.(*vm.ScopeContext)
code := scopeContext.Contract.Code
codeSize := len(code)
isCreate := callFrameState.create
gethCodeHash := scopeContext.Contract.CodeHash
if codeSize > 0 {

// Obtain our contract coverage map lookup hash.
if callFrameState.lookupHash == nil {
lookupHash := getContractCoverageMapHash(code, callFrameState.create)
lookupHash, cacheHit := t.codeHashCache[isCreate][gethCodeHash]
if !cacheHit {
lookupHash = getContractCoverageMapHash(code, isCreate)
t.codeHashCache[isCreate][gethCodeHash] = lookupHash
}
callFrameState.lookupHash = &lookupHash
}

Expand Down