pcopy icon indicating copy to clipboard operation
pcopy copied to clipboard

Feature Request: enable preheat by default

Open FlyingOnion opened this issue 1 year ago • 1 comments

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:

  1. split getFromCacheSetAndRun into get and run, the get function returns (*allFieldFunc, bool)
  2. if get returns nil, false, call newAllFieldFunc and saveToCache
  3. call *allFieldFunc.do
  4. the opts ...Option argument can be removed

What do you think? @guonaihong

FlyingOnion avatar Nov 09 '23 07:11 FlyingOnion

看你是国人。那就用中文回答了。当pcopy足够稳定的时候会默认启用预热。 我压测过,每次使用时都调用Preheat和Copy函数和单调Copy时间类似,因为预热函数会检查类型是否会有缓存,如果存在缓存就会直接跳过。

   pcopy.Preheat(&dst{}, &src{}) // 一对类型只要预热一次
   pcopy.Copy(&d, &s, pcopy.WithUsePreheat())

所以对于你的需求来说,实现一个万能的高性能deepcopy函数。先检查是否实现DeepCopyGen接口,如果没有实现直接调用Preheat和Copy和启动预热实际是一样,只是多调用了一行代码。

guonaihong avatar Nov 11 '23 06:11 guonaihong