diff --git a/erigon-lib/state/domain_shared.go b/erigon-lib/state/domain_shared.go index 251c8eaa0da..b632f82bdb8 100644 --- a/erigon-lib/state/domain_shared.go +++ b/erigon-lib/state/domain_shared.go @@ -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 @@ -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 { @@ -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) @@ -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 @@ -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 @@ -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) { @@ -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)) }