Skip to content

Commit

Permalink
up README
Browse files Browse the repository at this point in the history
  • Loading branch information
petersunbag committed Jan 31, 2018
1 parent e149932 commit cdf355c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[![Build Status](https://travis-ci.org/petersunbag/coven.svg?branch=master)](https://travis-ci.org/petersunbag/coven)
[![Coverage Status](https://coveralls.io/repos/github/petersunbag/coven/badge.svg?branch=master&98)](https://coveralls.io/github/petersunbag/coven?branch=master)

support struct-to-struct, slice-to-slice and map-to-map converting.
this project is inspired by https://github.com/thrift-iterator/go
Support struct-to-struct, slice-to-slice and map-to-map converting.
This package is inspired by https://github.com/thrift-iterator/go
* struct converting only affects target fields of the same name with source fields, the rest will remain unchanged.nested anonymous fields are supported.
* map converting only affects target with keys that source map has, the rest will remain unchanged.
* slice converting will overwrite the whole target slice.
Expand Down Expand Up @@ -49,7 +49,18 @@ func demo(){
// Output:
// {"A":[1,2,3],"B":{"1":"ab","2":"ba","3":"cd"},"C":6,"D":11}
```
## Benchmark ##

Direct ptr operation is faster than using reflection for basic types, such as int to float conversion, and even for identical slice or struct.

| ptr int-float | reflection int-float | ptr struct | reflection struct | ptr slice | reflection slice |
| --- | --- | --- | --- | --- | --- |
| 57.9 ns/op | 98.6 ns/op | 86.9 ns/op | 118 ns/op | 80.2 ns/op | 99.7 ns/op |
| 0 B/op | 16 B/op | 0 B/op | 64 B/op | 0 B/op | 32 B/op |
| 0 allocs/op | 2 allocs/op | 0 allocs/op | 1 allocs/op | 0 allocs/op| 1 allocs/op |

Test cases above don't include map type, because there is no way to operate map through ptr in Go.
See benchmark_test.go for details.
## License ##

This package is licensed under MIT license. See LICENSE for details.
23 changes: 18 additions & 5 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,22 @@ func BenchmarkStructConvert(b *testing.B) {
}

func BenchmarkSameStruct(b *testing.B) {
type foo struct {
type bar struct {
A int
B byte
}

type foo struct {
bar
C string
D []int
}


Foo := foo{}

c := NewConverter(Foo, Foo)
foo1 := &foo{1, 2}
foo1 := &foo{bar{1,2}, "abc", []int{1,2,3}}
foo2 := foo{}

for i := 0; i < b.N; i++ {
Expand All @@ -112,12 +119,18 @@ func BenchmarkSameStruct(b *testing.B) {
}

func BenchmarkSameStructReflect(b *testing.B) {
type foo struct {
type bar struct {
A int
B byte
}

foo1 := foo{1, 2}
type foo struct {
bar
C string
D []int
}

foo1 := foo{bar{1,2}, "abc", []int{1,2,3}}
foo2 := foo{}
t := reflect.TypeOf(foo1)

Expand Down Expand Up @@ -163,6 +176,6 @@ func BenchmarkBasicReflect(b *testing.B) {
t := reflect.TypeOf(y)

for i := 0; i < b.N; i++ {
y = reflect.ValueOf(x).Convert(t).Float()
reflect.ValueOf(&y).Elem().Set(reflect.ValueOf(x).Convert(t))
}
}

0 comments on commit cdf355c

Please sign in to comment.