Commit ebdefa2
committed
perf(chunkreader): avoid buffer pool thrashing on reset
The reset path unconditionally swaps oversized buffers back to minBufSize, causing repeated pool Get/Put cycles when workloads alternate between small and large reads.
Add 4x threshold before downsizing - keeps modestly oversized buffers, only reclaims when truly excessive. Also cache *r.buf to local variable to reduce pointer dereferences in hot path.
LargeSmallAlternating (16KB/100B cycles, 500 iterations):
Original: 450µs 1.05MB/op 131 allocs/op
Optimized: 47µs 16KB/op 4 allocs/op
Result: 9.5x faster, 98% less allocation
RandomSizes (1-16KB, 1000 iterations):
Original: 13.6ms 6.5MB/op 1015 allocs/op
Optimized: 11.5ms 98KB/op 13 allocs/op
Result: 1.2x faster, 98% less allocation
PGMessagePattern (5B header + 1KB body, 1000 iterations):
Original: 30µs 8KB/op 4 allocs/op
Optimized: 28µs 8KB/op 4 allocs/op
Result: ~1x (no thrashing in baseline)
Signed-off-by: Mathias Bogaert <mathias.bogaert@gmail.com>1 parent e214c23 commit ebdefa2
1 file changed
Lines changed: 21 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
46 | 49 | | |
47 | 50 | | |
48 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
49 | 55 | | |
50 | 56 | | |
| 57 | + | |
51 | 58 | | |
52 | 59 | | |
53 | 60 | | |
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
57 | 64 | | |
58 | | - | |
59 | | - | |
60 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
61 | 69 | | |
62 | 70 | | |
63 | 71 | | |
64 | | - | |
| 72 | + | |
65 | 73 | | |
66 | | - | |
| 74 | + | |
67 | 75 | | |
68 | 76 | | |
69 | 77 | | |
| 78 | + | |
70 | 79 | | |
71 | 80 | | |
72 | 81 | | |
73 | 82 | | |
74 | | - | |
75 | | - | |
| 83 | + | |
| 84 | + | |
76 | 85 | | |
77 | 86 | | |
78 | 87 | | |
79 | 88 | | |
80 | | - | |
| 89 | + | |
81 | 90 | | |
82 | | - | |
83 | 91 | | |
84 | 92 | | |
85 | 93 | | |
86 | 94 | | |
87 | | - | |
88 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
89 | 98 | | |
90 | 99 | | |
0 commit comments