- Notifications
You must be signed in to change notification settings - Fork12
pcopy是深度拷贝库,相比上个版本(v0.0.10),性能提升4-10倍
License
NotificationsYou must be signed in to change notification settings
antlabs/pcopy
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
pcopy.Copy
主要用于两个类型间的深度拷贝, 前身是deepcopy
新加预热函数。Copy时打开加速开关,达到性能提升4-10倍的效果。
警告:
高性能的同时可能会有些bug, 如果发现bug可以去掉pcopy.WithUsePreheat()
试下, 结果不一致,可以提issue。
- 高性能, 相对第一个版本提升4-10倍的性能
- 支持异构结构体拷贝, dst和src可以是不同的类型,会拷贝dst和src交集的部分
- 多类型支持struct/map/slice/array/int...int64/uint...uint64/ 等等
go get github.com/antlabs/pcopy
package mainimport ("fmt""github.com/antlabs/pcopy")typedststruct {IDintResultstring}typesrcstruct{IDintTextstring}funcmain() {d,s:=dst{},src{ID:3}pcopy.Preheat(&dst{},&src{})// 一对类型只要预热一次pcopy.Copy(&d,&s,pcopy.WithUsePreheat())fmt.Printf("%#v\n",d) }
package mainimport ("fmt""github.com/antlabs/pcopy")funcmain() {i:= []int{1,2,3,4,5,6}varo []intpcopy.Preheat(&o,&i)pcopy.Copy(&o,&i,pcopy.WithUsePreheat())fmt.Printf("%#v\n",o)}
package mainimport ("fmt""github.com/antlabs/pcopy")funcmain() {i:=map[string]int{"cat":100,"head":10,"tr":3,"tail":44, }varomap[string]intpcopy.Preheat(&o,&i)pcopy.Copy(&o,&i,pcopy.WithUsePreheat())fmt.Printf("%#v\n",o)}
经常看到,对同一个结构体的,有值更新操作,都是一堆手工if 然后赋值的代码。不仅容易出错,还累。快使用pcopy解放双手。
typeoptionstruct {IntintFloat64float64Sstring}funcmain() {vara,boptionifb.Int!=0 {a.Int=b.Int }ifb.Float64!=0.0 {a.Float64=b.Float64 }ifb.S!="" {a.S=b.S }pcopy.Preheat(&a,&b)//只要预热一次//可以约化成pcopy.Copy(&a,&b,pcopy.WithUsePreheat())}
从零实现的pcopy相比json序列化与反序列化方式拥有更好的性能
goos: darwingoarch: arm64pkg: benchmarkBenchmark_Use_reflectValue_MiniCopy-8 334728 3575 ns/opBenchmark_Use_reflectValue_DeepCopy-8 595302 1956 ns/opBenchmark_Use_reflectValue_Copier-8 203574 5860 ns/opBenchmark_Use_Ptr_jsoniter-8 821113 1477 ns/opBenchmark_Use_Ptr_pcopy-8 3390382 354.0 ns/opBenchmark_Use_Ptr_coven-8 1414197 848.7 ns/opPASSok benchmark9.771s
从下面的压测数据可以看到,基本提供了4-10倍的性能提升
goos: darwingoarch: arm64pkg: github.com/antlabs/pcopyBenchmark_BaseMap_Unsafe_Pcopy-8 529747 2343 ns/opBenchmark_BaseMap_miniCopy-8 62181 19212 ns/opBenchmark_BaseMap_Reflect-8 93810 12756 ns/opBenchmark_BaseSlice_Unsafe_Pcopy-8 2013764 595.1 ns/opBenchmark_BaseSlice_miniCopy-8 154918 7728 ns/opBenchmark_BaseSlice_Reflect-8 188720 6393 ns/opBenchmark_BaseType_Unsafe_Pcopy-8 4872112 243.8 ns/opBenchmark_BaseType_MiniCopy-8 517814 2278 ns/opBenchmark_BaseType_Pcopy-8 635156 1886 ns/opBenchmark_CompositeMap_Unsafe_Pcopy-8 486253 2409 ns/opBenchmark_CompositeMap_miniCopy-8 229674 5173 ns/opBenchmark_CompositeMap_Reflect-8 475243 2490 ns/opBenchmark_GetLikeFavorited_Unsafe_Pcopy2-8 446907 2662 ns/opBenchmark_GetLikeFavorited_Unsafe_Pcopy-8 470217 2572 ns/opBenchmark_GetLikeFavorited_MiniCopy-8 85674 13989 ns/opBenchmark_GetLikeFavorited_Reflect_Pcopy-8 121603 9856 ns/opBenchmark_GetRedPoint_Unsafe_Pcopy-8 1626688 736.1 ns/opBenchmark_GetRedPoint_MiniCopy-8 650004 1871 ns/opBenchmark_GetRedPoint_Reflect_Pcopy-8 1669778 722.0 ns/opBenchmark_Interface_Unsafe_Pcopy-8 2869022 421.3 ns/opBenchmark_Interface_MiniCopy-8 413936 2704 ns/opBenchmark_Interface_Pcopy-8 440250 2688 ns/opBenchmark_Interface_BaseSlice_Unsafe_Pcopy-8 1266501 947.4 ns/opBenchmark_Interface_BaseSlice_MiniCopy-8 141610 8422 ns/opBenchmark_Interface_BaseSlice_Pcopy-8 203906 5917 ns/opBenchmark_Ptr_BaseType1_Unsafe_Pcopy-8 910153 1310 ns/opBenchmark_Ptr_BaseType1_Reflect_Pcopy-8 391117 3026 ns/opBenchmark_Ptr_BaseSlice_Unsafe_Pcopy-8 698156 1704 ns/opBenchmark_Ptr_BaseSlice_Reflect_Pcopy-8 219999 5415 ns/opBenchmark_SliceWithStruct_Unsafe_Pcopy-8 1395982 860.3 ns/opBenchmark_SliceWithStruct_miniCopy-8 163154 7298 ns/opBenchmark_SliceWithStruct_Reflect_Pcopy-8 190728 6213 ns/op
About
pcopy是深度拷贝库,相比上个版本(v0.0.10),性能提升4-10倍
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published