From 9b58219eb7354bf094273d82382d57cd08def293 Mon Sep 17 00:00:00 2001 From: Gabriele Cimato Date: Tue, 8 Aug 2023 08:58:35 -0500 Subject: [PATCH] refactor: fix mem usage returns, update estimation functions --- array.go | 16 ++++++++-------- builtin_map.go | 2 +- object.go | 4 ++-- runtime.go | 42 ++++++++++++++++++++++-------------------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/array.go b/array.go index cb35708b..0e7a2a55 100644 --- a/array.go +++ b/array.go @@ -562,8 +562,8 @@ var ( // With this function we sample 10% of the array values, // determine their average mem usage and use that to estimate mem // usage of the whole array -func (a *arrayObject) estimateMemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsage uint64, err error) { - var samplesVisited, runningEstimate, newRunningEstimate uint64 +func (a *arrayObject) estimateMemUsage(ctx *MemUsageContext) (estimate uint64, newEstimate uint64, err error) { + var samplesVisited, memUsage, newMemUsage uint64 sampleSize := len(a.values) / 10 // grabbing one sample every "sampleSize" to provide consistent @@ -578,17 +578,17 @@ func (a *arrayObject) estimateMemUsage(ctx *MemUsageContext) (memUsage uint64, n memUsage += inc newMemUsage += newInc // average * number of a.values - runningEstimate = uint64((float32(memUsage) / float32(samplesVisited)) * float32(len(a.values))) - newRunningEstimate = uint64((float32(newMemUsage) / float32(samplesVisited)) * float32(len(a.values))) + estimate = uint64((float32(memUsage) / float32(samplesVisited)) * float32(len(a.values))) + newEstimate = uint64((float32(newMemUsage) / float32(samplesVisited)) * float32(len(a.values))) if err != nil { - return runningEstimate, newRunningEstimate, err + return estimate, newEstimate, err } - if exceeded := ctx.MemUsageLimitExceeded(memUsage); exceeded { - return memUsage, newMemUsage, nil + if exceeded := ctx.MemUsageLimitExceeded(estimate); exceeded { + return estimate, newEstimate, nil } } - return runningEstimate, newRunningEstimate, nil + return estimate, newEstimate, nil } func (a *arrayObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsage uint64, err error) { diff --git a/builtin_map.go b/builtin_map.go index de148662..5d3cecd5 100644 --- a/builtin_map.go +++ b/builtin_map.go @@ -101,7 +101,7 @@ func (mo *mapObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsag ctx.VisitObj(mo) if err := ctx.Descend(); err != nil { - return 0, 0, err + return memUsage, newMemUsage, err } memUsage, newMemUsage, err = mo.baseObject.MemUsage(ctx) diff --git a/object.go b/object.go index f4004c29..4b172f13 100644 --- a/object.go +++ b/object.go @@ -1932,8 +1932,8 @@ func computeMemUsageEstimate(memUsage, samplesVisited uint64, totalProps int) ui // estimateMemUsage helps calculating mem usage for large objects. // It will sample the object and use those samples to estimate the // mem usage. -func (o *baseObject) estimateMemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsage uint64, err error) { - var samplesVisited uint64 +func (o *baseObject) estimateMemUsage(ctx *MemUsageContext) (estimate uint64, newEstimate uint64, err error) { + var samplesVisited, memUsage, newMemUsage uint64 totalProps := len(o.propNames) sampleSize := totalProps / 10 diff --git a/runtime.go b/runtime.go index 48a2ef0d..d5ce876a 100644 --- a/runtime.go +++ b/runtime.go @@ -526,34 +526,36 @@ func (r *Runtime) MemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsage u } } - if r.vm != nil { - if r.vm.callStack != nil { - for idx := range r.vm.callStack { - inc, newInc, err := r.vm.callStack[idx].MemUsage(ctx) - memUsage += inc - newMemUsage += newInc - if err != nil { - return memUsage, newMemUsage, err - } - } - } + if r.vm == nil { + return memUsage, newMemUsage, nil + } - if r.vm.stash != nil { - inc, newInc, err := r.vm.stash.MemUsage(ctx) + if r.vm.callStack != nil { + for idx := range r.vm.callStack { + inc, newInc, err := r.vm.callStack[idx].MemUsage(ctx) memUsage += inc newMemUsage += newInc if err != nil { return memUsage, newMemUsage, err } } + } - if r.vm.stack != nil { - inc, newInc, err := r.vm.stack.MemUsage(ctx) - memUsage += inc - newMemUsage += newInc - if err != nil { - return memUsage, newMemUsage, err - } + if r.vm.stash != nil { + inc, newInc, err := r.vm.stash.MemUsage(ctx) + memUsage += inc + newMemUsage += newInc + if err != nil { + return memUsage, newMemUsage, err + } + } + + if r.vm.stack != nil { + inc, newInc, err := r.vm.stack.MemUsage(ctx) + memUsage += inc + newMemUsage += newInc + if err != nil { + return memUsage, newMemUsage, err } }