From 943073d0ccf810ff7e26f88677c829a33184ff1e Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Sat, 2 Nov 2024 20:44:45 +0200 Subject: [PATCH] perf: do not use TotalVoices as it causes heap allocations --- patch.go | 6 +++++- song.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/patch.go b/patch.go index f9b7cd2..a8db243 100644 --- a/patch.go +++ b/patch.go @@ -422,7 +422,11 @@ func (p Patch) FirstVoiceForInstrument(instrIndex int) int { return 0 } instrIndex = min(instrIndex, len(p)) - return TotalVoices(p[:instrIndex]) + ret := 0 + for i := 0; i < instrIndex; i++ { + ret += p[i].NumVoices + } + return ret } // InstrumentForVoice returns the instrument number for the given voice index. diff --git a/song.go b/song.go index d89f0fc..ab1ad95 100644 --- a/song.go +++ b/song.go @@ -275,7 +275,11 @@ func (l Score) FirstVoiceForTrack(track int) int { return 0 } track = min(track, len(l.Tracks)) - return TotalVoices(l.Tracks[:track]) + ret := 0 + for i := 0; i < track; i++ { + ret += l.Tracks[i].NumVoices + } + return ret } // LengthInRows returns just RowsPerPattern * Length, as the length is the