-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
utils: add Go 1.20+ way of converting string to byte slice (#2462)
* utils: add Go 1.20+ way of converting string to byte slice Ref. https://github.com/valyala/fasthttp/blob/d2f97fc426ed451e64dc8e35e7f87a1d4a2d7bde/s2b_old.go. Ref. https://github.com/valyala/fasthttp/blob/d2f97fc426ed451e64dc8e35e7f87a1d4a2d7bde/s2b_new.go. * utils: fix golangci-lint apparently running with Go < 1.20 See https://github.com/gofiber/fiber/actions/runs/4968641325/jobs/8891360463?pr=2462.
- Loading branch information
1 parent
77c1b48
commit eced39c
Showing
3 changed files
with
38 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build go1.20 | ||
// +build go1.20 | ||
|
||
package utils | ||
|
||
import ( | ||
"unsafe" | ||
) | ||
|
||
// UnsafeBytes returns a byte pointer without allocation. | ||
func UnsafeBytes(s string) []byte { | ||
return unsafe.Slice(unsafe.StringData(s), len(s)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build !go1.20 | ||
// +build !go1.20 | ||
|
||
package utils | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
const MaxStringLen = 0x7fff0000 // Maximum string length for UnsafeBytes. (decimal: 2147418112) | ||
|
||
// UnsafeBytes returns a byte pointer without allocation. | ||
// String length shouldn't be more than 2147418112. | ||
// | ||
//nolint:gosec // unsafe is used for better performance here | ||
func UnsafeBytes(s string) []byte { | ||
if s == "" { | ||
return nil | ||
} | ||
|
||
return (*[MaxStringLen]byte)(unsafe.Pointer( | ||
(*reflect.StringHeader)(unsafe.Pointer(&s)).Data), | ||
)[:len(s):len(s)] | ||
} |
eced39c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold
2
.Benchmark_AcquireCtx
1613
ns/op 1568 B/op 5 allocs/op616.8
ns/op 1568 B/op 5 allocs/op2.62
This comment was automatically generated by workflow using github-action-benchmark.