Skip to content

Commit

Permalink
refactor: fix mem usage returns, update estimation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabri3l committed Aug 8, 2023
1 parent 9f60a09 commit 9b58219
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
16 changes: 8 additions & 8 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion builtin_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 22 additions & 20 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 9b58219

Please sign in to comment.