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

为什么返回请求数据需要做深拷贝呢?代码中存在多余的new #71

Open
KelvinChen684 opened this issue Dec 4, 2020 · 0 comments

Comments

@KelvinChen684
Copy link

为什么每次返回请求数据都要new分配内存呢?直接返回新建/查询到的数据不行吗?如:
func (a *App) NewInstance(ni *Instance, latestTime int64) (i *Instance, ok bool) {
i = new(Instance)
a.lock.Lock()
...
*i = *ni
...
}
func (a *App) Cancel(hostname string, latestTime int64) (i *Instance, l int, ok bool) {
i = new(Instance)
oi, ok := a.instances[hostname]
...
*i = *oi
}
且多处返回空数据也会使用new,如此会产生大量gc吧。如:
func (a *App) Renew(hostname string) (i *Instance, ok bool) {
i = new(Instance)
a.lock.Lock()
defer a.lock.Unlock()
oi, ok := a.instances[hostname]
if !ok {
return // i = new(Instance) 应放在此if之后吧?
}
oi.RenewTimestamp = time.Now().UnixNano()
i = copyInstance(oi)
return
}

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

1 participant