Skip to content

Commit

Permalink
commitment.EncodeBranch: rely on external copy (#12916)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Nov 29, 2024
1 parent 96b8655 commit dd5c46f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions erigon-lib/state/domain_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,8 @@ func (sd *SharedDomains) put(domain kv.Domain, key string, val []byte) {
// get returns cached value by key. Cache is invalidated when associated WAL is flushed
func (sd *SharedDomains) get(table kv.Domain, key []byte) (v []byte, prevStep uint64, ok bool) {
//sd.muMaps.RLock()
keyS := *(*string)(unsafe.Pointer(&key))
keyS := toStringZeroCopy(key)
var dataWithPrevStep dataWithPrevStep
//keyS := string(key)
if table == kv.StorageDomain {
dataWithPrevStep, ok = sd.storage.Get(keyS)
return dataWithPrevStep.data, dataWithPrevStep.prevStep, ok
Expand Down Expand Up @@ -619,9 +618,9 @@ func (sd *SharedDomains) updateAccountCode(addr, code, prevCode []byte, prevStep
return sd.domainWriters[kv.CodeDomain].PutWithPrev(addr, nil, code, prevCode, prevStep)
}

func (sd *SharedDomains) updateCommitmentData(prefix []byte, data, prev []byte, prevStep uint64) error {
sd.put(kv.CommitmentDomain, string(prefix), data)
return sd.domainWriters[kv.CommitmentDomain].PutWithPrev(prefix, nil, data, prev, prevStep)
func (sd *SharedDomains) updateCommitmentData(prefix string, data, prev []byte, prevStep uint64) error {
sd.put(kv.CommitmentDomain, prefix, data)
return sd.domainWriters[kv.CommitmentDomain].PutWithPrev([]byte(prefix), nil, data, prev, prevStep)
}

func (sd *SharedDomains) deleteAccount(addr, prev []byte, prevStep uint64) error {
Expand Down Expand Up @@ -1064,7 +1063,7 @@ func (sd *SharedDomains) DomainDel(domain kv.Domain, k1, k2 []byte, prevVal []by
}
return sd.updateAccountCode(k1, nil, prevVal, prevStep)
case kv.CommitmentDomain:
return sd.updateCommitmentData(k1, nil, prevVal, prevStep)
return sd.updateCommitmentData(toStringZeroCopy(k1), nil, prevVal, prevStep)
default:
sd.put(domain, string(append(k1, k2...)), nil)
return sd.domainWriters[domain].DeleteWithPrev(k1, k2, prevVal, prevStep)
Expand Down Expand Up @@ -1153,7 +1152,8 @@ func (sdc *SharedDomainsCommitmentContext) ResetBranchCache() {
}

func (sdc *SharedDomainsCommitmentContext) Branch(pref []byte) ([]byte, uint64, error) {
cached, ok := sdc.branches[string(pref)]
prefixS := string(pref)
cached, ok := sdc.branches[prefixS]
if ok {
// cached value is already transformed/clean to read.
// Cache should ResetBranchCache after each commitment computation
Expand All @@ -1169,7 +1169,7 @@ func (sdc *SharedDomainsCommitmentContext) Branch(pref []byte) ([]byte, uint64,
}
// Trie reads prefix during unfold and after everything is ready reads it again to Merge update, if any, so
// cache branch until ResetBranchCache called
sdc.branches[string(pref)] = cachedBranch{data: v, step: step}
sdc.branches[prefixS] = cachedBranch{data: v, step: step}

if len(v) == 0 {
return nil, 0, nil
Expand All @@ -1178,12 +1178,13 @@ func (sdc *SharedDomainsCommitmentContext) Branch(pref []byte) ([]byte, uint64,
}

func (sdc *SharedDomainsCommitmentContext) PutBranch(prefix []byte, data []byte, prevData []byte, prevStep uint64) error {
prefixS := toStringZeroCopy(prefix)
if sdc.sharedDomains.trace {
fmt.Printf("[SDC] PutBranch: %x: %x\n", prefix, data)
}
sdc.branches[string(prefix)] = cachedBranch{data: data, step: prevStep}
sdc.branches[prefixS] = cachedBranch{data: data, step: prevStep}

return sdc.sharedDomains.updateCommitmentData(prefix, data, prevData, prevStep)
return sdc.sharedDomains.updateCommitmentData(prefixS, data, prevData, prevStep)
}

func (sdc *SharedDomainsCommitmentContext) Account(plainKey []byte) (u *commitment.Update, err error) {
Expand Down Expand Up @@ -1466,3 +1467,5 @@ func (sdc *SharedDomainsCommitmentContext) restorePatriciaState(value []byte) (u
}
return cs.blockNum, cs.txNum, nil
}

func toStringZeroCopy(v []byte) string { return *(*string)(unsafe.Pointer(&v)) }

0 comments on commit dd5c46f

Please sign in to comment.