Commit 90b66c3
committed
perf(utils): single-buffer ID() + shortRand — 5 allocs → 1
ID() built its 30-char output by chaining four allocating helpers:
Concat → FormatUint → RandomBytes → HexEncode, each allocating an
intermediate string that was discarded by the next stage.
Rewrite: build into a single 32-byte slice via strconv.AppendUint +
hex.AppendEncode, hand to AsString for the zero-copy return. Same
crypto-random entropy source (cryptorand.Read into a 3-byte stack
buffer) so the security shape is unchanged.
shortRand() same pattern — one alloc instead of two.
Bench impact (Apple M3 Ultra):
Utils_ID 128.8 ns / 64 B / 5 allocs → 72.89 ns / 32 B / 1 alloc
1.8x faster, 80% alloc reduction1 parent 7d1874e commit 90b66c3
1 file changed
Lines changed: 31 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
8 | 14 | | |
9 | 15 | | |
10 | 16 | | |
| |||
14 | 20 | | |
15 | 21 | | |
16 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
17 | 28 | | |
18 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
19 | 42 | | |
20 | 43 | | |
| 44 | + | |
| 45 | + | |
21 | 46 | | |
22 | | - | |
23 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
24 | 50 | | |
25 | 51 | | |
26 | | - | |
| 52 | + | |
| 53 | + | |
27 | 54 | | |
28 | 55 | | |
29 | 56 | | |
| |||
0 commit comments