Skip to content

Boxing In Hot Path (ARCH5087)

Key go.performance.boxing-in-hot-path · Severity info · Category performance

Wrapping values in interface{} (any) inside a tight loop forces a heap allocation on every iteration. Keep concrete types on hot paths.

func sum(nums []int) int {
var total int
for _, n := range nums {
var boxed any = n
total += boxed.(int)
}
return total
}
func sum(nums []int) int {
var total int
for _, n := range nums {
total += n
}
return total
}

Remove interface{}/any wrapping in hot loops and use the concrete type directly.