Skip to content

Commit

Permalink
refactor: ensure objectGoSlice and objectGoMapSimple only apply to ne…
Browse files Browse the repository at this point in the history
…w mem usage
  • Loading branch information
Gabri3l committed Aug 30, 2023
1 parent a30da41 commit d4837d7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 34 deletions.
54 changes: 27 additions & 27 deletions builtin_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,33 +253,33 @@ func TestMapObjectMemUsage(t *testing.T) {
expectedNewMem uint64
errExpected error
}{
// {
// name: "mem below threshold",
// mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
// mo: &mapObject{
// m: &orderedMap{
// hashTable: map[uint64]*mapEntry{
// 1: {
// key: vm.ToValue("key"),
// value: vm.ToValue("value"),
// },
// },
// },
// },
// // baseObject + (len(key) + overhead) + (len(value) + overhead)
// expectedMem: SizeEmptyStruct + (3 + SizeString) + (5 + SizeString),
// // baseObject + (len(key) + overhead) + (len(value) + overhead)
// expectedNewMem: SizeEmptyStruct + (3 + SizeString) + (5 + SizeString),
// errExpected: nil,
// },
// {
// name: "mem is SizeEmptyStruct given a nil map object",
// mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
// mo: nil,
// expectedMem: SizeEmptyStruct,
// expectedNewMem: SizeEmptyStruct,
// errExpected: nil,
// },
{
name: "mem below threshold",
mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
mo: &mapObject{
m: &orderedMap{
hashTable: map[uint64]*mapEntry{
1: {
key: vm.ToValue("key"),
value: vm.ToValue("value"),
},
},
},
},
// baseObject + (len(key) + overhead) + (len(value) + overhead)
expectedMem: SizeEmptyStruct + (3 + SizeString) + (5 + SizeString),
// baseObject + (len(key) + overhead) + (len(value) + overhead)
expectedNewMem: SizeEmptyStruct + (3 + SizeString) + (5 + SizeString),
errExpected: nil,
},
{
name: "mem is SizeEmptyStruct given a nil map object",
mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
mo: nil,
expectedMem: SizeEmptyStruct,
expectedNewMem: SizeEmptyStruct,
errExpected: nil,
},
{
name: "mem way above threshold returns first crossing of threshold",
mu: NewMemUsageContext(vm, 88, 100, 50, 50, TestNativeMemUsageChecker{}),
Expand Down
23 changes: 18 additions & 5 deletions object_goslice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ func TestGoSliceMemUsage(t *testing.T) {
expectedNewMem: SizeEmptyStruct + SizeInt*2,
errExpected: nil,
},
{
name: "should account for baseObject only given a nil data field",
val: &objectGoSlice{
baseObject: baseObject{
val: &Object{runtime: vm},
},
},
// overhead
expectedMem: SizeEmptyStruct,
// overhead
expectedNewMem: SizeEmptyStruct,
errExpected: nil,
},
{
name: "should account for each value given a slice with go ints",
val: &objectGoSlice{
Expand Down Expand Up @@ -342,8 +355,8 @@ func TestGoSliceMemUsage(t *testing.T) {
},
},
},
// overhead + (value + len("length") with string overhead + "length".value + prototype + ints)
expectedMem: SizeEmptyStruct + (SizeEmptyStruct + (6 + SizeString) + SizeEmptyStruct + (SizeEmptyStruct + SizeEmptyStruct) + SizeNumber*2),
// default + default since we don't account for objectGoSlice in (*Object).MemUsage
expectedMem: SizeEmptyStruct + SizeEmptyStruct,
// overhead + (value + len("length") with string overhead + "length".value + prototype + ints)
expectedNewMem: SizeEmptyStruct + (SizeEmptyStruct + (6 + SizeString) + SizeEmptyStruct + (SizeEmptyStruct + SizeEmptyStruct) + SizeNumber*2),
errExpected: nil,
Expand All @@ -361,8 +374,8 @@ func TestGoSliceMemUsage(t *testing.T) {
},
},
},
// overhead + (value + len("length") with string overhead + "length".value + prototype + ints)
expectedMem: SizeEmptyStruct + (SizeEmptyStruct + (6 + SizeString) + SizeEmptyStruct + (SizeEmptyStruct + SizeEmptyStruct) + SizeNumber*2),
// default + default since we don't account for objectGoSlice in (*Object).MemUsage
expectedMem: SizeEmptyStruct + SizeEmptyStruct,
// overhead + (value + len("length") with string overhead + "length".value + prototype + ints)
expectedNewMem: SizeEmptyStruct + (SizeEmptyStruct + (6 + SizeString) + SizeEmptyStruct + (SizeEmptyStruct + SizeEmptyStruct) + SizeNumber*2),
errExpected: nil,
Expand All @@ -371,7 +384,7 @@ func TestGoSliceMemUsage(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
total, newTotal, err := tc.val.MemUsage(NewMemUsageContext(vm, 100, 100, 100, 100, nil))
total, newTotal, err := tc.val.MemUsage(NewMemUsageContext(vm, 100, 100000, 100, 100, nil))
if err != tc.errExpected {
t.Fatalf("Unexpected error. Actual: %v Expected: %v", err, tc.errExpected)
}
Expand Down
6 changes: 6 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,12 @@ func (o *Object) MemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsage ui
return SizeEmptyStruct, SizeEmptyStruct, nil
case *objectGoSliceReflect:
return SizeEmptyStruct, SizeEmptyStruct, nil
case *objectGoMapSimple:
_, newMemUsage, err := x.MemUsage(ctx)
return SizeEmptyStruct, newMemUsage, err
case *objectGoSlice:
_, newMemUsage, err := x.MemUsage(ctx)
return SizeEmptyStruct, newMemUsage, err
default:
r, ok := x.(MemUsageReporter)
if !ok {
Expand Down
35 changes: 33 additions & 2 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func TestValuePropertyMemUsage(t *testing.T) {
}

func TestObjectMemUsage(t *testing.T) {
vm := New()
tests := []struct {
name string
val *Object
Expand Down Expand Up @@ -244,19 +245,49 @@ func TestObjectMemUsage(t *testing.T) {
errExpected: nil,
},
{
name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoMapSimple",
name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoMapSimple empty",
val: &Object{self: &objectGoMapSimple{}},
expectedMem: SizeEmptyStruct,
expectedNewMem: SizeEmptyStruct,
errExpected: nil,
},
{
name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoSlice",
name: "should have different values given an Object with self of type objectGoMapSimple non empty",
val: &Object{self: &objectGoMapSimple{
baseObject: baseObject{
val: &Object{runtime: vm},
},
data: map[string]interface{}{
"test0": valueInt(99),
"test1": valueInt(99),
},
}},
expectedMem: SizeEmptyStruct,
expectedNewMem: SizeEmptyStruct + ((5+SizeString)+SizeInt)*2,
errExpected: nil,
},
{
name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoSlice empty",
val: &Object{self: &objectGoSlice{}},
expectedMem: SizeEmptyStruct,
expectedNewMem: SizeEmptyStruct,
errExpected: nil,
},
{
name: "should have different values given an Object with self of type objectGoSlice non empty",
val: &Object{self: &objectGoSlice{
baseObject: baseObject{
val: &Object{runtime: vm},
},
data: &[]interface{}{
valueInt(99),
valueInt(99),
},
}},
expectedMem: SizeEmptyStruct,
expectedNewMem: SizeEmptyStruct + SizeInt*2,
errExpected: nil,
},
{
name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoSliceReflect",
val: &Object{self: &objectGoSliceReflect{}},
Expand Down

0 comments on commit d4837d7

Please sign in to comment.