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.
This commit is contained in:
5684185+vsariola@users.noreply.github.com 2023-10-21 10:47:26 +03:00
parent 1040eb585d
commit 3c85f1155c

View File

@ -16,14 +16,15 @@ import (
) )
type VSTIProcessContext struct { type VSTIProcessContext struct {
events []vst2.MIDIEvent events []vst2.MIDIEvent
host vst2.Host eventIndex int
host vst2.Host
} }
func (c *VSTIProcessContext) NextEvent() (event tracker.MIDINoteEvent, ok bool) { func (c *VSTIProcessContext) NextEvent() (event tracker.MIDINoteEvent, ok bool) {
var ev vst2.MIDIEvent for c.eventIndex < len(c.events) {
for len(c.events) > 0 { ev := c.events[c.eventIndex]
ev, c.events = c.events[0], c.events[1:] c.eventIndex++
switch { switch {
case ev.Data[0] >= 0x80 && ev.Data[0] < 0x90: case ev.Data[0] >= 0x80 && ev.Data[0] < 0x90:
channel := ev.Data[0] - 0x80 channel := ev.Data[0] - 0x80
@ -66,7 +67,7 @@ func init() {
tracker := gioui.NewTracker(model, cmd.MainSynther) tracker := gioui.NewTracker(model, cmd.MainSynther)
tracker.SetInstrEnlarged(true) // start the vsti with the instrument editor enlarged tracker.SetInstrEnlarged(true) // start the vsti with the instrument editor enlarged
go tracker.Main() go tracker.Main()
context := VSTIProcessContext{make([]vst2.MIDIEvent, 100), h} context := VSTIProcessContext{host: h}
buf := make(sointu.AudioBuffer, 1024) buf := make(sointu.AudioBuffer, 1024)
return vst2.Plugin{ return vst2.Plugin{
UniqueID: PLUGIN_ID, UniqueID: PLUGIN_ID,
@ -88,7 +89,8 @@ func init() {
for i := 0; i < out.Frames; i++ { for i := 0; i < out.Frames; i++ {
left[i], right[i] = buf[i][0], buf[i][1] 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{ }, vst2.Dispatcher{
CanDoFunc: func(pcds vst2.PluginCanDoString) vst2.CanDoResponse { CanDoFunc: func(pcds vst2.PluginCanDoString) vst2.CanDoResponse {