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

perf: higher performance unsafeStringToSlice #371

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions nocopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package netpoll

import (
"io"
"reflect"
"unsafe"

"github.com/bytedance/gopkg/lang/dirtmake"
Expand Down Expand Up @@ -276,13 +275,13 @@ func unsafeSliceToString(b []byte) string {
}

// zero-copy slice convert to string
func unsafeStringToSlice(s string) (b []byte) {
p := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b))
hdr.Data = uintptr(p)
hdr.Cap = len(s)
hdr.Len = len(s)
return b
func unsafeStringToSlice(s string) []byte {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dxasu you should remove "reflect" import path also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return *(*[]byte)(unsafe.Pointer(
&struct {
string
int
}{s, len(s)},
))
}

// malloc limits the cap of the buffer from mcache.
Expand Down
Loading