From 3c85f1155cadb2e2ba33f5d2184761b1fb562d89 Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Sat, 21 Oct 2023 10:47:26 +0300 Subject: [PATCH] perf(cmd/sointu-vsti): avoid reallocations of events array Always appending to the end and consuming from the front cause the capacity of the slice regularly running out, resulting in new allocation. With this change, we increment index when consuming events and append to the end, and when we reset, we move index to 0 and empty slice. This way, we always reuse the allocated memory. --- cmd/sointu-vsti/main.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/sointu-vsti/main.go b/cmd/sointu-vsti/main.go index 32bcf62..ded6e7b 100644 --- a/cmd/sointu-vsti/main.go +++ b/cmd/sointu-vsti/main.go @@ -16,14 +16,15 @@ import ( ) type VSTIProcessContext struct { - events []vst2.MIDIEvent - host vst2.Host + events []vst2.MIDIEvent + eventIndex int + host vst2.Host } func (c *VSTIProcessContext) NextEvent() (event tracker.MIDINoteEvent, ok bool) { - var ev vst2.MIDIEvent - for len(c.events) > 0 { - ev, c.events = c.events[0], c.events[1:] + for c.eventIndex < len(c.events) { + ev := c.events[c.eventIndex] + c.eventIndex++ switch { case ev.Data[0] >= 0x80 && ev.Data[0] < 0x90: channel := ev.Data[0] - 0x80 @@ -66,7 +67,7 @@ func init() { tracker := gioui.NewTracker(model, cmd.MainSynther) tracker.SetInstrEnlarged(true) // start the vsti with the instrument editor enlarged go tracker.Main() - context := VSTIProcessContext{make([]vst2.MIDIEvent, 100), h} + context := VSTIProcessContext{host: h} buf := make(sointu.AudioBuffer, 1024) return vst2.Plugin{ UniqueID: PLUGIN_ID, @@ -88,7 +89,8 @@ func init() { for i := 0; i < out.Frames; i++ { left[i], right[i] = buf[i][0], buf[i][1] } - context.events = context.events[:0] + context.events = context.events[:0] // reset buffer, but keep the allocated memory + context.eventIndex = 0 }, }, vst2.Dispatcher{ CanDoFunc: func(pcds vst2.PluginCanDoString) vst2.CanDoResponse {