some optimization direction when use []byte ,string ,fmt.Spintf.
liu-song opened this issue · 0 comments
Is your feature request related to a problem? Please describe.
Not a problem,just some optimization.
Describe the solution you'd like
when we transform []byte to string or string to []byte, below way will more faster:
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func s2b(s string) (b []byte) {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
return b
}
the function is Copied from fasthttp ,https://github.com/valyala/fasthttp/blob/master/bytesconv.go#L333 , which also is used in gin https://github.com/gin-gonic/gin/blob/master/internal/bytesconv/bytesconv.go#L12 and so on .
when we use the function fmt.Spintf to concatenated string #891,it will case more because use interface ,reflect , append . it is more suitable to use strings.Builder or bytes.Buffer .
In the bfe can make some performances improvement in these cases.
Describe alternatives you've considered
when we use b2s or s2b ,it also need pay more attention because unsafe . fasthttp has some discussion on this case;
valyala/fasthttp#1031
valyala/fasthttp#1151
Additional context
Add any other context or screenshots about the feature request here.