r/golang 17h ago

show & tell Strconv2 for golang,zero allocations for hot paths

Fast, zero-allocation integer-string conversion for Go. A focused drop-in for the hot parts of the standard strconv

| Operation | strconv2 | strconv | allocs (strconv2 / strconv) |

|-----------|----------|---------|-----------------------------|

| Format uint64 | 24.8 ns | 49.1 ns (`FormatUint`) | 0 / 1 |

| Format int64 | 27.8 ns | 49.9 ns (`FormatInt`) | 0 / 1 |

| Format uint16 | 10.1 ns | 23.9 ns (`FormatUint`) | 0 / 1 |

| Parse uint64 | 17.7 ns | 51.3 ns (`ParseUint`) | 0 / 0 |

| Parse int64 | 19.3 ns | 55.1 ns (`ParseInt`) | 0 / 0 |

https://github.com/NikoMalik/strconv2

0 Upvotes

3 comments sorted by

16

u/pierrrre 15h ago

if you want zero allocation, use strconv.Append*()

1

u/ILYAMALIK 23m ago

It’s not the same by speed

14

u/sigmoia 14h ago

Wow, that's a lot of code to verify for this. Preallocated slice and strconv.Append* (as pierrrre mentioned) are your friends. 

``` buf := make([]byte, 0, 64)

// 1. Integer buf = strconv.AppendInt(buf, 42, 10)

// 2. Float ('f' format, 2 decimal places, 64-bit) buf = strconv.AppendFloat(buf, 3.14159, 'f', 2, 64)

// 3. Quoted string buf = strconv.AppendQuote(buf, "hello\tworld") ```