You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
gin/internal/bytesconv/bytesconv_1.20.go
Lines 15 to 17 in 0397e5e
gin/internal/bytesconv/bytesconv_test.go
Lines 22 to 24 in 0397e5e
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: #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)The text was updated successfully, but these errors were encountered: