Skip to content

Change deprecated reflect function in unsafeString2Bytes#27

Open
1neZe0 wants to merge 1 commit into
valyala:masterfrom
1neZe0:my-changes
Open

Change deprecated reflect function in unsafeString2Bytes#27
1neZe0 wants to merge 1 commit into
valyala:masterfrom
1neZe0:my-changes

Conversation

@1neZe0

@1neZe0 1neZe0 commented Aug 28, 2023

Copy link
Copy Markdown
  1. Replace deprecated reflect functions:
    https://pkg.go.dev/reflect#SliceHeader
    https://pkg.go.dev/reflect#StringHeader
  2. Simpler - there's no need to manipulate or types.
  3. Faster because directly convert the type without creating temporary structures

https://pkg.go.dev/reflect#SliceHeader
https://pkg.go.dev/reflect#StringHeader
2. Simpler - there's no need to manipulate  or  types.
3. Faster because directly convert the type without creating temporary structures
Comment thread unsafe.go
bh.Cap = sh.Len
bh.Len = sh.Len
return b
return *(*[]byte)(unsafe.Pointer(&s))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you probably want

func String2ByteSlice(str string) []byte {
	if str == "" {
		return nil
	}
	return unsafe.Slice(unsafe.StringData(str), len(str))
}

(from https://go101.org/article/unsafe.html)

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

Successfully merging this pull request may close these issues.

2 participants