From 97a1b2f7661b37f6b66f6ce72533590674534611 Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Sun, 15 Oct 2023 11:11:26 +0300 Subject: [PATCH] perf(tracker): use json recovery files instead of yaml for less garbage The yaml marshaling and umarshaling seems to allocate a lot of memory. When saving the recovery file, the memory use jumped up by hundreds of megabytes. Switch to using json marshaling for the recovery file, as it does waste memory so badly. Binary marshaling was also an option, but its nice in emergency situations that the user can glance the recovery file and perhaps, with some effort, recover stuff from it. Json is good enough for manual recovery during emergency situations. --- tracker/model.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tracker/model.go b/tracker/model.go index ea156e2..7123de9 100644 --- a/tracker/model.go +++ b/tracker/model.go @@ -1,6 +1,7 @@ package tracker import ( + "encoding/json" "errors" "fmt" "math" @@ -12,7 +13,7 @@ import ( "github.com/vsariola/sointu" "github.com/vsariola/sointu/vm" "golang.org/x/exp/slices" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" ) // Model implements the mutable state for the tracker program GUI. @@ -113,7 +114,7 @@ const ( ) const maxUndo = 64 -const RECOVERY_FILE = ".sointu_recovery.yml" +const RECOVERY_FILE = ".sointu_recovery" func NewModel(modelMessages chan<- interface{}, playerMessages <-chan PlayerMessage) *Model { ret := new(Model) @@ -135,10 +136,14 @@ func LoadRecovery(modelMessages chan<- interface{}, playerMessages <-chan Player return nil, fmt.Errorf("could not read recovery file: %w", err) } var ret Model - err = yaml.Unmarshal(b, &ret.d) + err = json.Unmarshal(b, &ret.d) if err != nil { - return nil, fmt.Errorf("could not unmarshal recovery file: %w", err) + err = yaml.Unmarshal(b, &ret.d) + if err != nil { + return nil, fmt.Errorf("could not unmarshal recovery file: %w", err) + } } + ret.modelMessages = modelMessages ret.PlayerMessages = playerMessages ret.notifyPatchChange() @@ -152,7 +157,7 @@ func (m *Model) SaveRecovery() error { if err != nil { return fmt.Errorf("could not get user home directory: %w", err) } - out, err := yaml.Marshal(m.d) + out, err := json.Marshal(m.d) if err != nil { return fmt.Errorf("could not marshal the model: %w", err) }