domains / app-frameworks / gofiber/fiber
minimize memory allocations
Prioritize reducing memory allocations and choosing efficient data structures to improve performance. Avoid unnecessary string/byte conversions, use maps for O(1) lookups instead of slices, and leverage compiler optimizations where possible.
Prioritize reducing memory allocations and choosing efficient data structures to improve performance. Avoid unnecessary string/byte conversions, use maps for O(1) lookups instead of slices, and leverage compiler optimizations where possible.
Key strategies:
- Use
utils.EqualFoldinstead ofutils.ToLowerfor case-insensitive comparisons to avoid allocations - Choose maps over slices for lookups:
map[int]boolinstead of[]intfor O(1) performance - Preallocate slices with appropriate capacity to avoid runtime growth
- Combine string operations to reduce allocations:
strings.ToLower(scheme + "://" + host)instead of separate calls - Leverage Go compiler optimizations for
[]bytetostringcomparisons - Use library-specific optimized functions like
fasthttp.ParseFloatandUnsafeByteswhen available
Example:
// Inefficient - multiple allocations
if utils.ToLower(cookie.SameSite) == CookieSameSiteNoneMode {
// process
}
// Efficient - no allocations
if utils.EqualFold(cookie.SameSite, CookieSameSiteNoneMode) {
// process
}
// Inefficient - O(n) lookup
var cacheableStatusCodes = []int{200, 203, 300, 301}
if slices.Contains(cacheableStatusCodes, status) { /* ... */ }
// Efficient - O(1) lookup
var cacheableStatusCodes = map[int]bool{200: true, 203: true, 300: true, 301: true}
if cacheableStatusCodes[status] { /* ... */ }