Detect slice re-creation and suggest re-use autofix instead?
dnwe opened this issue · 0 comments
dnwe commented
I wonder if we can reliably suggest slice re-use rather than re-initialisation?
e.g., taking the example from the the Thanos coding-style-guide
var messages []string{}
for _, msg := range recv {
messages = append(messages, msg)
if len(messages) > maxMessageLen {
marshalAndSend(messages)
// This creates new array. Previous array
// will be garbage collected only after
// some time (seconds), which
// can create enormous memory pressure.
messages = []string{}
}
}
=>
var messages []string{}
for _, msg := range recv {
messages = append(messages, msg)
if len(messages) > maxMessageLen {
marshalAndSend(messages)
// Instead of new array, reuse
// the same, with the same capacity,
// just length equals to zero.
messages = messages[:0]
}
}