From 5d32ea1eb4c891587c3693aabe8263afc8837cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Thu, 1 Feb 2024 15:47:15 +0100 Subject: [PATCH] Refactor stackframe construction to reduce memory pressure --- server/pkg/wfexec/state.go | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/server/pkg/wfexec/state.go b/server/pkg/wfexec/state.go index 10ed2aad8..ee15169e3 100644 --- a/server/pkg/wfexec/state.go +++ b/server/pkg/wfexec/state.go @@ -1,7 +1,7 @@ package wfexec import ( - "encoding/json" + "sync" "time" "github.com/cortezaproject/corteza/server/pkg/auth" @@ -136,10 +136,13 @@ func (s State) MakeFrame() *Frame { // might not be the most optimal way but we need to // un-reference scope, input, result variables unref = func(vars *expr.Vars) *expr.Vars { - out := &expr.Vars{} - tmp, _ := json.Marshal(vars) - _ = json.Unmarshal(tmp, out) - return out + aux, err := vars.Clone() + if err != nil { + return expr.EmptyVars() + } + + // Since we're cloning vars, this will always hold + return aux.(*expr.Vars) } ) @@ -147,13 +150,30 @@ func (s State) MakeFrame() *Frame { CreatedAt: s.created, SessionID: s.sessionId, StateID: s.stateId, - Input: unref(s.input), - Scope: unref(s.scope), - Results: unref(s.results), NextSteps: s.next.IDs(), Action: s.action, } + var wg sync.WaitGroup + wg.Add(3) + + go func() { + f.Input = unref(s.input) + wg.Done() + }() + + go func() { + f.Scope = unref(s.scope) + wg.Done() + }() + + go func() { + f.Results = unref(s.results) + wg.Done() + }() + + wg.Wait() + if s.err != nil { f.Error = s.err.Error() }