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 1 commit
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
14 changes: 7 additions & 7 deletions nocopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import (
"io"
"reflect"

Check failure on line 19 in nocopy.go

View workflow job for this annotation

GitHub Actions / golangci-lint

"reflect" imported and not used (typecheck)

Check failure on line 19 in nocopy.go

View workflow job for this annotation

GitHub Actions / golangci-lint

"reflect" imported and not used) (typecheck)

Check failure on line 19 in nocopy.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.18, X64)

imported and not used: "reflect"

Check failure on line 19 in nocopy.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.18, ARM64)

imported and not used: "reflect"

Check failure on line 19 in nocopy.go

View workflow job for this annotation

GitHub Actions / windows-test

"reflect" imported and not used

Check failure on line 19 in nocopy.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.23, X64)

"reflect" imported and not used
"unsafe"

"github.com/bytedance/gopkg/lang/dirtmake"
Expand Down Expand Up @@ -276,13 +276,13 @@
}

// 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