Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
And found that both methods have 0 memory allocation and the raw conversion is a little faster than the unsafe one:
╰─± go test -v -run=none -bench=^BenchmarkBytesConvStr -benchmem=true
goos: darwin
goarch: amd64
pkg: github.com/gin-gonic/gin/internal/bytesconv
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkBytesConvStrToBytesRaw
BenchmarkBytesConvStrToBytesRaw-12 1000000000 0.2571 ns/op 0 B/op 0 allocs/op
BenchmarkBytesConvStrToBytes
BenchmarkBytesConvStrToBytes-12 1000000000 0.5216 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/gin-gonic/gin/internal/bytesconv 1.104s
The repo uses the unsafe conversion in a few places at this moment, which may not be necessary at all. 2 reasons why we don't want to use the unsafe version:
The raw conversion is slightly faster than the unsafe version
Description
I ran the given benchmark in bytesconv_test.go to compare the performance of converting a string to bytes using:
And found that both methods have 0 memory allocation and the raw conversion is a little faster than the unsafe one:
The repo uses the unsafe conversion in a few places at this moment, which may not be necessary at all. 2 reasons why we don't want to use the unsafe version:
The PR to fix: https://github.com/gin-gonic/gin/pull/3936
How to reproduce
cd internal/bytesconv
go test -v -run=none -bench=^BenchmarkBytesConvStr -benchmem=true
Expectations
Actual result
raw conversion is slightly faster than unsafe conversion. Both have zero memory allocation.
Environment
go version go1.22.2 darwin/amd64
[0397e5e](https://github.com/gin-gonic/gin/commit/0397e5e0c0f8f8176c29f7edd8f1bff8e45df780)
darwin/amd64
(macOS)