Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: enable preheat by default #19

Open
FlyingOnion opened this issue Nov 9, 2023 · 1 comment
Open

Feature Request: enable preheat by default #19

FlyingOnion opened this issue Nov 9, 2023 · 1 comment

Comments

@FlyingOnion
Copy link

FlyingOnion commented Nov 9, 2023

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

@guonaihong
Copy link
Contributor

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants