Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rlp: add functions for encoding #257

Merged
merged 2 commits into from
Jan 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions rlp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,9 @@ type field struct {
}

func makeStructDecoder(typ reflect.Type) (decoder, error) {
var fields []field
for i := 0; i < typ.NumField(); i++ {
if f := typ.Field(i); f.PkgPath == "" { // exported
info, err := cachedTypeInfo1(f.Type)
if err != nil {
return nil, err
}
fields = append(fields, field{i, info})
}
fields, err := structFields(typ)
if err != nil {
return nil, err
}
dec := func(s *Stream, val reflect.Value) (err error) {
if _, err = s.List(); err != nil {
Expand Down
22 changes: 12 additions & 10 deletions rlp/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"math/big"
"reflect"
"testing"

"github.com/ethereum/go-ethereum/ethutil"
)

func TestStreamKind(t *testing.T) {
Expand Down Expand Up @@ -509,13 +507,13 @@ func ExampleStream() {
}

func BenchmarkDecode(b *testing.B) {
enc := encTest(90000)
enc := encodeTestSlice(90000)
b.SetBytes(int64(len(enc)))
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
var s []int
var s []uint
r := bytes.NewReader(enc)
if err := Decode(r, &s); err != nil {
b.Fatalf("Decode error: %v", err)
Expand All @@ -524,12 +522,12 @@ func BenchmarkDecode(b *testing.B) {
}

func BenchmarkDecodeIntSliceReuse(b *testing.B) {
enc := encTest(100000)
enc := encodeTestSlice(100000)
b.SetBytes(int64(len(enc)))
b.ReportAllocs()
b.ResetTimer()

var s []int
var s []uint
for i := 0; i < b.N; i++ {
r := bytes.NewReader(enc)
if err := Decode(r, &s); err != nil {
Expand All @@ -538,12 +536,16 @@ func BenchmarkDecodeIntSliceReuse(b *testing.B) {
}
}

func encTest(n int) []byte {
s := make([]interface{}, n)
for i := 0; i < n; i++ {
func encodeTestSlice(n uint) []byte {
s := make([]uint, n)
for i := uint(0); i < n; i++ {
s[i] = i
}
return ethutil.Encode(s)
b, err := EncodeToBytes(s)
if err != nil {
panic(fmt.Sprintf("encode error: %v", err))
}
return b
}

func unhex(str string) []byte {
Expand Down
Loading