erigon-pulse/trie/rlphacks/bytes_test.go
ledgerwatch dca36e8b29
Restore the functionality CalcTrieRoots (compute trie root without modifying the trie) (#327)
* Trace first block

* Fixes for CalcTrieRoots

* Timings of the CalcTrieRoot

* Fix lint

* Add memory profiling

* Reduce allocations in StreamHash

* Fix

* Fix

* Fix

* Optimise allocations

* Reuse streams

* Fixes

* Fix

* Unit test fix

* Fix lint

* Reuse hashbuilder

* No loop

* Reuse resolver

* Fixed tests

* Fix test

* Fix test

* Fix test

* Fix witness threshold

* Optimise allocations in RLP transform

* Optimise allocations in RLP transform

* Optimise branchHash

* 100 times again

* Replace ToStream with Iterator

* StreamMergeIterator

* No streams

* Amplification

* Minimise the use of hashOnly

* 100 times

* Reduce stack operations

* Reduce appends

* More optimisations

* More optimisations

* More optimisations

* local hex

* Small fix

* Remove test

* Fix lint

* Fix lint

* Fix lint

* Add test for empty

* Fix lint

* More tests

* Fix lint

* Add measurement of stateless exec
2020-01-30 13:16:12 +00:00

66 lines
2.0 KiB
Go

package rlphacks
import (
"bytes"
"testing"
"github.com/ledgerwatch/turbo-geth/rlp"
)
func TestFastDoubleRlpForByteArrays(t *testing.T) {
for i := 0; i < 256; i++ {
doTestWithByte(t, byte(i), 1)
}
doTestWithByte(t, 0x0, 100000)
doTestWithByte(t, 0xC, 100000)
doTestWithByte(t, 0xAB, 100000)
}
func doTestWithByte(t *testing.T, b byte, iterations int) {
var prefixBuf [8]byte
buffer := new(bytes.Buffer)
for i := 0; i < iterations; i++ {
buffer.WriteByte(b)
source := buffer.Bytes()
encSingle, _ := rlp.EncodeToBytes(source)
encDouble, _ := rlp.EncodeToBytes(encSingle)
if RlpSerializableBytes(source).DoubleRLPLen() != len(encDouble) {
t.Errorf("source [%2x * %d] wrong RlpSerializableBytes#DoubleRLPLen prediction: %d (expected %d)", source[0], len(source), RlpSerializableBytes(source).DoubleRLPLen(), len(encDouble))
}
if RlpEncodedBytes(encSingle).DoubleRLPLen() != len(encDouble) {
t.Errorf("source [%2x * %d] wrong RlpEncodedBytes#DoubleRLPLen prediction: %d (expected %d)", source[0], len(source), RlpEncodedBytes(encSingle).DoubleRLPLen(), len(encDouble))
}
buffDouble := new(bytes.Buffer)
if err := RlpSerializableBytes(source).ToDoubleRLP(buffDouble, prefixBuf[:]); err != nil {
t.Errorf("test failed, err = %v", err)
}
buffSingle := new(bytes.Buffer)
if err := RlpEncodedBytes(encSingle).ToDoubleRLP(buffSingle, prefixBuf[:]); err != nil {
t.Errorf("test failed, err = %v", err)
}
if !bytes.Equal(buffDouble.Bytes(), encDouble) {
t.Errorf("source [%2x * %d] wrong RlpSerializableBytes#ToDoubleRLP prediction: %x (expected %x)", source[0], len(source), displayOf(buffDouble.Bytes()), displayOf(encDouble))
}
if !bytes.Equal(buffSingle.Bytes(), encDouble) {
t.Errorf("source [%2x * %d] wrong RlpEncodedBytes#ToDoubleRLP prediction: %x (expected %x)", source[0], len(source), displayOf(buffSingle.Bytes()), displayOf(encDouble))
}
}
}
func displayOf(bytes []byte) []byte {
if len(bytes) < 20 {
return bytes
}
return bytes[:20]
}