Skip to content

Commit

Permalink
Misc GC optimisations (erigontech#619)
Browse files Browse the repository at this point in the history
* uint256 in rlp

* uint256 rather than big.Int in Transation

* linters

* more linters

* still linters

* Reduce garbage in writeUint256

* Experiment with GC in writeByteArray

* Misc GC optimisations

* unsafe experiment with writeByteArray
  • Loading branch information
yperbasis authored Jun 4, 2020
1 parent b20cdbe commit 05e8118
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
7 changes: 4 additions & 3 deletions common/dbutils/composite_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ func ParseCompositeStorageKey(compositeKey []byte) (common.Hash, uint64, common.
// AddrHash + incarnation + KeyHash
// For contract storage (for plain state)
func PlainGenerateCompositeStorageKey(address common.Address, incarnation uint64, key common.Hash) []byte {
compositeKey := make([]byte, 0, common.AddressLength+8+common.HashLength)
compositeKey = append(compositeKey, PlainGenerateStoragePrefix(address, incarnation)...)
compositeKey = append(compositeKey, key[:]...)
compositeKey := make([]byte, common.AddressLength+8+common.HashLength)
copy(compositeKey, address[:])
binary.BigEndian.PutUint64(compositeKey[common.AddressLength:], ^incarnation)
copy(compositeKey[common.AddressLength+8:], key[:])
return compositeKey
}

Expand Down
1 change: 1 addition & 0 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m
if current.Eq(value) { // noop (1)
return params.SstoreNoopGasEIP2200, nil
}

var original uint256.Int
evm.IntraBlockState.GetCommittedState(contract.Address(), &key, &original)
if original == current {
Expand Down
30 changes: 21 additions & 9 deletions rlp/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"math/big"
"reflect"
"sync"
"unsafe"

"github.com/holiman/uint256"
)
Expand Down Expand Up @@ -114,10 +115,10 @@ func EncodeToReader(val interface{}) (size int, r io.Reader, err error) {
}

type encbuf struct {
str []byte // string data, contains everything except list headers
lheads []*listhead // all list headers
lhsize int // sum of sizes of all encoded list headers
sizebuf []byte // 9-byte auxiliary buffer for uint encoding
str []byte // string data, contains everything except list headers
lheads []listhead // all list headers
lhsize int // sum of sizes of all encoded list headers
sizebuf []byte // 9-byte auxiliary buffer for uint encoding
}

type listhead struct {
Expand Down Expand Up @@ -203,13 +204,15 @@ func (w *encbuf) encodeString(b []byte) {
}
}

func (w *encbuf) list() *listhead {
lh := &listhead{offset: len(w.str), size: w.lhsize}
func (w *encbuf) list() int {
lh := listhead{offset: len(w.str), size: w.lhsize}
idx := len(w.lheads)
w.lheads = append(w.lheads, lh)
return lh
return idx
}

func (w *encbuf) listEnd(lh *listhead) {
func (w *encbuf) listEnd(idx int) {
lh := &w.lheads[idx]
lh.size = w.size() - lh.offset - lh.size
if lh.size < 56 {
w.lhsize++ // length encoded into kind tag
Expand Down Expand Up @@ -481,7 +484,16 @@ func writeByteArray(val reflect.Value, w *encbuf) error {
pos := len(w.str)
w.str = append(w.str, make([]byte, size)...)
slice := w.str[pos:]
reflect.Copy(reflect.ValueOf(slice), val)
if val.CanAddr() {
sh := &reflect.SliceHeader{
Data: val.UnsafeAddr(),
Len: size,
Cap: size,
}
copy(slice, *(*[]byte)(unsafe.Pointer(sh)))
} else {
reflect.Copy(reflect.ValueOf(slice), val)
}
return nil
}

Expand Down

0 comments on commit 05e8118

Please sign in to comment.