Permalink
Browse files

Merge pull request #1412 from NebulousLabs/hashall

Shorter HashAll
  • Loading branch information...
2 parents fb3d05b + 78f4851 commit 63082950b2809f7218d5a80a0db9103ee0e83760 @DavidVorick DavidVorick committed on GitHub Aug 17, 2016
Showing with 22 additions and 8 deletions.
  1. +1 −8 crypto/hash.go
  2. +21 −0 encoding/marshal_test.go
View
@@ -41,14 +41,7 @@ func NewHash() hash.Hash {
// HashAll takes a set of objects as input, encodes them all using the encoding
// package, and then hashes the result.
func HashAll(objs ...interface{}) Hash {
- // Ideally we would just write HashBytes(encoding.MarshalAll(objs)).
- // Unfortunately, you can't pass 'objs' to MarshalAll without losing its
- // type information; MarshalAll would just see interface{}s.
- var b []byte
- for _, obj := range objs {
- b = append(b, encoding.Marshal(obj)...)
- }
- return HashBytes(b)
+ return HashBytes(encoding.MarshalAll(objs...))
}
// HashBytes takes a byte slice and returns the result.
@@ -401,3 +401,24 @@ func BenchmarkDecode(b *testing.B) {
}
b.SetBytes(numBytes)
}
+
+// i5-4670K, 2059112: 44 MB/s
+func BenchmarkMarshalAll(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ _ = MarshalAll(testStructs...)
+ }
+ b.SetBytes(int64(len(bytes.Join(testEncodings, nil))))
+}
+
+// i5-4670K, 2059112: 36 MB/s
+func BenchmarkUnmarshalAll(b *testing.B) {
+ var emptyStructs = []interface{}{&test0{}, &test1{}, &test2{}, &test3{}, &test4{}, &test5{}, &test6{}}
+ structBytes := bytes.Join(testEncodings, nil)
+ for i := 0; i < b.N; i++ {
+ err := UnmarshalAll(structBytes, emptyStructs...)
+ if err != nil {
+ b.Fatal(err)
+ }
+ }
+ b.SetBytes(int64(len(structBytes)))
+}

0 comments on commit 6308295

Please sign in to comment.