pcopy
pcopy copied to clipboard
Feature Request: enable preheat by default
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:
- split
getFromCacheSetAndRun
intoget
andrun
, theget
function returns(*allFieldFunc, bool)
- if
get
returnsnil, false
, callnewAllFieldFunc
andsaveToCache
- call
*allFieldFunc.do
- the
opts ...Option
argument can be removed
What do you think? @guonaihong
看你是国人。那就用中文回答了。当pcopy足够稳定的时候会默认启用预热。 我压测过,每次使用时都调用Preheat和Copy函数和单调Copy时间类似,因为预热函数会检查类型是否会有缓存,如果存在缓存就会直接跳过。
pcopy.Preheat(&dst{}, &src{}) // 一对类型只要预热一次
pcopy.Copy(&d, &s, pcopy.WithUsePreheat())
所以对于你的需求来说,实现一个万能的高性能deepcopy函数。先检查是否实现DeepCopyGen接口,如果没有实现直接调用Preheat和Copy和启动预热实际是一样,只是多调用了一行代码。