@@ -105,9 +105,10 @@ type scanResult struct {
105105}
106106
107107// scanResultPoolCapacity is the pre-allocated slice capacity for pooled scanResult objects.
108- // 256 chunks = ~16KB input (256 * 64 bytes per chunk) - covers most typical CSV files.
109- // Increased from 64 to reduce ensureUint64SliceCap reallocations observed in profiling.
110- const scanResultPoolCapacity = 256
108+ // 2048 chunks = ~128KB input (2048 * 64 bytes per chunk) - covers most typical CSV files.
109+ // Increased from 256 to reduce ensureUint64SliceCap reallocations observed in profiling.
110+ // For 10K-row CSV (~600KB), this covers ~13% with initial capacity, reducing reallocs.
111+ const scanResultPoolCapacity = 2048
111112
112113// scanResultPool provides reusable scanResult objects to reduce allocations.
113114var scanResultPool = sync.Pool {
@@ -153,14 +154,20 @@ func releaseScanResult(sr *scanResult) {
153154
154155// ensureUint64SliceCap ensures slice has at least required length with 2x growth.
155156// Returns the slice with length set to required.
157+ // Note: This creates a new slice if capacity is insufficient; the old slice's
158+ // capacity is lost but this is acceptable since pooled objects start with
159+ // sufficient capacity (scanResultPoolCapacity) for most use cases.
156160func ensureUint64SliceCap (s []uint64 , required int ) []uint64 {
157161 if cap (s ) >= required {
158162 return s [:required ]
159163 }
164+ // Use max of 2x growth or required capacity
160165 newCap := cap (s ) * 2
161166 if newCap < required {
162167 newCap = required
163168 }
169+ // Add 25% headroom to reduce future reallocations
170+ newCap += newCap / 4
164171 return make ([]uint64 , required , newCap )
165172}
166173
@@ -172,10 +179,13 @@ func ensureBoolSliceCap(s []bool, required int) []bool {
172179 clear (s )
173180 return s
174181 }
182+ // Use max of 2x growth or required capacity
175183 newCap := cap (s ) * 2
176184 if newCap < required {
177185 newCap = required
178186 }
187+ // Add 25% headroom to reduce future reallocations
188+ newCap += newCap / 4
179189 return make ([]bool , required , newCap )
180190}
181191
0 commit comments