Skip to content

Commit

Permalink
common, contract: optimize save memory contract snapshot (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-urth authored and iwasaki-kenta committed Sep 2, 2019
1 parent c4b9450 commit d5f5990
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
2 changes: 2 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ var (
ZeroSignature Signature

ZeroRoundPtr = &Round{}

ZeroPage = make([]byte, PageSize)
)
38 changes: 14 additions & 24 deletions contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package wavelet

import (
"bytes"
"crypto/sha256"
"crypto/sha512"
"encoding/binary"
Expand All @@ -38,7 +39,6 @@ import (
)

var (
ErrNotSmartContract = errors.New("contract: specified account ID is not a smart contract")
ErrContractFunctionNotFound = errors.New("contract: smart contract func not found")

_ exec.ImportResolver = (*ContractExecutor)(nil)
Expand Down Expand Up @@ -445,39 +445,29 @@ func SaveContractMemorySnapshot(snapshot *avl.Tree, id AccountID, mem []byte) {

WriteAccountContractNumPages(snapshot, id, numPages)

var (
identical, allZero bool
pageStart uint64
)

for pageIdx := uint64(0); pageIdx < numPages; pageIdx++ {
old, _ := ReadAccountContractPage(snapshot, id, pageIdx)
identical, allZero = true, false
pageStart = pageIdx * PageSize

identical := true

for idx := uint64(0); idx < PageSize; idx++ {
if len(old) == 0 && mem[pageIdx*PageSize+idx] != 0 {
identical = false
break
}

if len(old) != 0 && mem[pageIdx*PageSize+idx] != old[idx] {
identical = false
break
}
if len(old) == 0 {
allZero = bytes.Equal(ZeroPage, mem[pageStart:pageStart+PageSize])
identical = allZero
} else {
identical = bytes.Equal(old, mem[pageStart:pageStart+PageSize])
}

if !identical {
allZero := true

for idx := uint64(0); idx < PageSize; idx++ {
if mem[pageIdx*PageSize+idx] != 0 {
allZero = false
break
}
}

// If the page is empty, save an empty byte array. Otherwise, save the pages content.

if allZero {
WriteAccountContractPage(snapshot, id, pageIdx, []byte{})
} else {
WriteAccountContractPage(snapshot, id, pageIdx, mem[pageIdx*PageSize:(pageIdx+1)*PageSize])
WriteAccountContractPage(snapshot, id, pageIdx, mem[pageStart:pageStart+PageSize])
}
}
}
Expand Down

0 comments on commit d5f5990

Please sign in to comment.