MCPcopy Index your code
hub / github.com/coder/coder / BenchmarkStringUnsafe20

Function BenchmarkStringUnsafe20

cryptorand/strings_test.go:144–173  ·  view source on GitHub ↗
(b *testing.B)

Source from the content-addressed store, hash-verified

142}
143
144func BenchmarkStringUnsafe20(b *testing.B) {
145 mkstring := func(charSetStr string, size int) (string, error) {
146 charSet := []rune(charSetStr)
147
148 // This buffer facilitates pre-emptively creation of random uint32s
149 // to reduce syscall overhead.
150 ibuf := make([]byte, 4*size)
151
152 _, err := rand.Read(ibuf)
153 if err != nil {
154 return "", err
155 }
156
157 var buf strings.Builder
158 buf.Grow(size)
159
160 for i := 0; i < size; i++ {
161 n := binary.BigEndian.Uint32(ibuf[i*4 : (i+1)*4])
162 _, _ = buf.WriteRune(charSet[n%uint32(len(charSet))]) // #nosec G115 - Safe conversion as len(charSet) will be reasonably small for character sets
163 }
164
165 return buf.String(), nil
166 }
167
168 b.SetBytes(20)
169 b.ReportAllocs()
170 for i := 0; i < b.N; i++ {
171 _, _ = mkstring(cryptorand.Default, 20)
172 }
173}
174
175func BenchmarkStringBigint20(b *testing.B) {
176 mkstring := func(charSetStr string, size int) (string, error) {

Callers

nothing calls this directly

Calls 2

ReadMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected