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

Unmarshal()使用了内存分配 #71

Closed
frank-yf opened this issue Aug 12, 2021 · 1 comment
Closed

Unmarshal()使用了内存分配 #71

frank-yf opened this issue Aug 12, 2021 · 1 comment

Comments

@frank-yf
Copy link

frank-yf commented Aug 12, 2021

func Unmarshal(buf []byte, val interface{}) error {
	return sonic.UnmarshalString(*(*string)(unsafe.Pointer(&buf)), val)
}

使用*(*string)(unsafe.Pointer(&buf))而不是string(buf),以减少内存分配。

假如考虑到byte数组的安全性,我认为不需要担心,使用sonic框架后,正常调用都是通过sonic.UnmarshalString完成的,项目中很少会用到sonic.Unmarshal。很多时候用到byte数组的序列化都是因为需要替换其他框架的序列化方式。

@chenzhuoyu
Copy link
Member

刻意这么设计的,为了和标准库保持一定兼容性。我们没办法保证别人不去修改 []byte 的内容。关于 UnmarshalString 这个已经在 README.md 中写得比较清楚了的。

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