Your question is Optimize Code for Performance. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
DAZN streams live sports and needs to buffer match events (goals, cards, VAR reviews) for delivery to connected clients during a live broadcast. The function below is used per match and starts consuming excessive memory and CPU during long matches with extra time. How would you optimize this given code snippet for better performance or memory usage?
func ProcessMatchEvents(events <-chan MatchEvent, sink chan<- string) {
var buffer []MatchEvent
for evt := range events {
buffer = append(buffer, evt)
payload := ""
for _, e := range buffer {
payload = payload + e.Type + ":" + e.Player + ";"
}
conn := openClientConn()
defer conn.Close()
go func() {
sink <- payload
}()
}
}
Walk through what you would change and why.