Skip to content

Commit

Permalink
refactor to use memlimit field
Browse files Browse the repository at this point in the history
  • Loading branch information
nickpoindexter committed Jul 31, 2023
1 parent 8a9e700 commit 92ff8ee
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 57 deletions.
8 changes: 4 additions & 4 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ func (a *arrayObject) estimateMemUsage(ctx *MemUsageContext) (uint64, error) {
if err != nil {
return runningEstimate, err
}
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
if exceeded := ctx.MemUsageLimitExceeded(total); exceeded {
return total, nil
}
}

Expand Down Expand Up @@ -633,8 +633,8 @@ func (a *arrayObject) MemUsage(ctx *MemUsageContext) (uint64, error) {
}
// This is an early exit in case we reach the mem usage
// limit before we get to scan the whole array.
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
if exceeded := ctx.MemUsageLimitExceeded(total); exceeded {
return total, nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions array_sparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ func (a *sparseArrayObject) MemUsage(ctx *MemUsageContext) (uint64, error) {
if err != nil {
return total, err
}
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
if exceeded := ctx.MemUsageLimitExceeded(total); exceeded {
return total, nil
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ func TestArrayObjectMemUsage(t *testing.T) {
maxDepth: 50,
},
NativeMemUsageChecker: &TestNativeMemUsageChecker{},
MemUsageExceedsLimit: func(memUsage uint64) bool {
// memory usage limit above which we should stop mem usage computations
return memUsage > 50
},
memoryLimit: 50,
ObjectPropsLenExceedsThreshold: func(objPropsLen int) bool {
// number of obj props beyond which we should estimate mem usage
return objPropsLen > 50
Expand Down
4 changes: 2 additions & 2 deletions builtin_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func (mo *mapObject) MemUsage(ctx *MemUsageContext) (uint64, error) {
return total, err
}
}
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
if exceeded := ctx.MemUsageLimitExceeded(total); exceeded {
return total, nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ func (p *Program) MemUsage(ctx *MemUsageContext) (uint64, error) {
if err != nil {
return total, err
}
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
if exceeded := ctx.MemUsageLimitExceeded(total); exceeded {
return total, nil
}
}

Expand Down
19 changes: 5 additions & 14 deletions mem_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ type MemUsageContext struct {
visitTracker
*depthTracker
NativeMemUsageChecker
MemUsageExceedsLimit func(memUsage uint64) bool
ArrayLenExceedsThreshold func(arrayLen int) bool
ObjectPropsLenExceedsThreshold func(objPropsLen int) bool
memoryLimit uint64
}

func NewMemUsageContext(
Expand All @@ -107,10 +107,7 @@ func NewMemUsageContext(
visitTracker: visitTracker{objsVisited: map[objectImpl]bool{}, stashesVisited: map[*stash]bool{}},
depthTracker: &depthTracker{curDepth: 0, maxDepth: maxDepth},
NativeMemUsageChecker: nativeChecker,
MemUsageExceedsLimit: func(memUsage uint64) bool {
// memory usage limit above which we should stop mem usage computations
return memUsage > memLimit
},
memoryLimit: memLimit,
ArrayLenExceedsThreshold: func(arrayLen int) bool {
// array length threshold above which we should estimate mem usage
return arrayLen > arrayLenThreshold
Expand All @@ -123,15 +120,9 @@ func NewMemUsageContext(
}

// MemUsageLimitExceeded ensures a limit function is defined and checks against the limit. If limit is breached
// it will return true, if the limit check is undefined it will return an error
func (m *MemUsageContext) MemUsageLimitExceeded(memUsage uint64) (bool, error) {
if m.MemUsageExceedsLimit == nil {
return false, errMemUsageExceedsLimitNil
}
if m.MemUsageExceedsLimit(memUsage) {
return true, nil
}
return false, nil
// it will return true
func (m *MemUsageContext) MemUsageLimitExceeded(memUsage uint64) bool {
return memUsage > m.memoryLimit
}

var (
Expand Down
38 changes: 9 additions & 29 deletions mem_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,35 @@ import "testing"

func TestMemUsageLimitExceeded(t *testing.T) {
tests := []struct {
name string
memUsage uint64
mu *MemUsageContext
expected bool
errExpected error
name string
memUsage uint64
mu *MemUsageContext
expected bool
}{
{
name: "did not exceed returns false",
memUsage: 12,
mu: &MemUsageContext{
MemUsageExceedsLimit: func(memUsage uint64) bool {
return memUsage > 50
},
memoryLimit: 50,
},
expected: false,
errExpected: nil,
},
{
name: "undefined function returns error",
memUsage: 12,
mu: &MemUsageContext{},
expected: false,
errExpected: errMemUsageExceedsLimitNil,
expected: false,
},
{
name: "memory exceeds threshold returns true",
memUsage: 700,
mu: &MemUsageContext{
MemUsageExceedsLimit: func(memUsage uint64) bool {
return memUsage > 50
},
memoryLimit: 50,
},
expected: true,
errExpected: nil,
expected: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actual, err := tc.mu.MemUsageLimitExceeded(tc.memUsage)
actual := tc.mu.MemUsageLimitExceeded(tc.memUsage)
if actual != tc.expected {
t.Fatalf("ACTUAL: %v EXPECTED: %v", actual, tc.expected)
}
if err == nil && tc.errExpected != nil || err != nil && tc.errExpected == nil {
t.Fatalf("Unexpected error. Actual: %v Expected; %v", err, tc.errExpected)
}
if err != nil && tc.errExpected != nil && err.Error() != tc.errExpected.Error() {
t.Fatalf("Errors do not match. Actual: %v Expected: %v", err, tc.errExpected)
}
})
}
}

0 comments on commit 92ff8ee

Please sign in to comment.