antlabs / pcopy Goto Github PK
View Code? Open in Web Editor NEWpcopy是深度拷贝库,相比上个版本(v0.0.10),性能提升4-10倍
License: MIT License
pcopy是深度拷贝库,相比上个版本(v0.0.10),性能提升4-10倍
License: MIT License
和src数据序列化成json。json反序列化到struct或者map,哪个快?
常见类型
6.15或者7月初
开始下个版本的性能优化,先尝试缓存解码器的思路。
使用指针优化
Thanks for your contribution, and I do have a suggestion which may make this library better.
I'm using this library to write a code generator with generics, and I cannot predict which specific type the user may call.
type DeepCopyGen[T any] interface {
DeepCopyInto(*T)
}
func DeepCopy[T any](in, out *T) {
var _in any = in
if d, ok := _in.(DeepCopyGen[T]); ok {
// use deepcopy-gen
d.DeepCopyInto(out)
return
}
// Have to call pcopy.Preheat each time
pcopy.Copy(in, out, pcopy.WithUsePreheat())
}
if pcopy.Copy
enables preheat by default, then things go easy. Just call pcopy.Copy(in, out)
is enough.
The process could be as follows:
getFromCacheSetAndRun
into get
and run
, the get
function returns (*allFieldFunc, bool)
get
returns nil, false
, call newAllFieldFunc
and saveToCache
*allFieldFunc.do
opts ...Option
argument can be removedWhat do you think? @guonaihong
已经加入检查策略
0.普通递归拷贝
deepcopy.Copy(dst, src).Do()
1.控制拷贝层次,下面结构体只拷贝一层
deepcopy.Copy().MaxDepth(1).Do()
2.只拷贝指定tag的字段
deepcopy.Copy().RegisterTagName("copy").Do()
3.只拷贝string和int类型
deepcopy.Copy().OnlyType(reflect.String, reflect.Int).Do()
4.支持字段选择
deepcopy.Copy().OnlyField("id", "idx").Do()
deepcopy.Copy().OnlyField(".struct2", ".struct3").Do()
5.支持效验器,不符合要求的数据不copy
deepcopy.Copy().NeeValid(deepcopy.String()).Do()
第3,4,5条暂时不实现。
import (
"testing"
"github.com/antlabs/deepcopy"
)
func TestPointerCopy(t *testing.T) {
type A struct {
p []int
}
src := A{[]int{1}}
t.Logf("%v", src)
var dst A
deepcopy.Copy(&dst, &src).Do()
t.Logf("%v", dst)
dst.p[0] = 2 // panic here
if src.p[0] == dst.p[0] {
t.Fail()
}
}
and result:
=== RUN TestPointerCopy
--- FAIL: TestPointerCopy (0.00s)
deepcopy_test.go:15: {[1]}
deepcopy_test.go:18: {[]}
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 7 [running]:
testing.tRunner.func1(0xc0000b2200)
/usr/lib/go-1.13/src/testing/testing.go:874 +0x3a3
panic(0x5cfc00, 0xc0000162a0)
/usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
golang_test.TestPointerCopy(0xc0000b2200)
/home/phenix/github/phenix3443/test/golang/deepcopy_test.go:19 +0x2a6
testing.tRunner(0xc0000b2200, 0x5f3bd0)
/usr/lib/go-1.13/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
/usr/lib/go-1.13/src/testing/testing.go:960 +0x350
FAIL golang_test 0.010s
FAIL
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.