Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAAS-23857: apply new mem usage everyhere but objectGoSlice and objectGoMapSimple #99

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion array.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@
}

func toIdx(v valueInt) uint32 {
if v >= 0 && v < math.MaxUint32 {

Check failure on line 550 in array.go

View workflow job for this annotation

GitHub Actions / test (1.16.x, ubuntu-latest, 386)

constant 4294967295 overflows valueInt
return uint32(v)
}
return math.MaxUint32
Expand Down Expand Up @@ -618,7 +618,7 @@

// slice overhead from a.values
if a.values != nil {
memUsage += 0
memUsage += SizeEmptySlice
newMemUsage += SizeEmptySlice
}

Expand Down
37 changes: 19 additions & 18 deletions array_sparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ func TestSparseArrayExportToSlice(t *testing.T) {
}

func TestSparseArrayObjectMemUsage(t *testing.T) {
vm := New()
tests := []struct {
name string
mu *MemUsageContext
Expand All @@ -274,64 +275,64 @@ func TestSparseArrayObjectMemUsage(t *testing.T) {
}{
{
name: "mem below threshold",
mu: NewMemUsageContext(New(), 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
sao: &sparseArrayObject{
items: []sparseArrayItem{
{
idx: 1,
value: New()._newString(newStringValue("key"), nil),
value: vm.ToValue("key"),
},
},
},
// array overhead + index size + stringObject + sparseArray baseObject
expected: SizeEmptyStruct + SizeInt32 + 25 + SizeEmptyStruct,
// array overhead + index size + stringObject + sparseArray baseObject
newExpected: SizeEmptyStruct + SizeInt32 + 57 + SizeEmptyStruct,
// array overhead + index size + (string len + overhead) + sparseArray baseObject
expected: SizeEmptyStruct + SizeInt32 + (3 + SizeString) + SizeEmptyStruct,
// array overhead + index size + (string len + overhead) + sparseArray baseObject
newExpected: SizeEmptyStruct + SizeInt32 + (3 + SizeString) + SizeEmptyStruct,
errExpected: nil,
},
{
name: "mem is SizeEmptyStruct for nil sparse array",
mu: NewMemUsageContext(New(), 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
sao: nil,
expected: SizeEmptyStruct,
newExpected: SizeEmptyStruct,
errExpected: nil,
},
{
name: "mem way above threshold returns first crossing of threshold",
mu: NewMemUsageContext(New(), 88, 100, 50, 50, TestNativeMemUsageChecker{}),
mu: NewMemUsageContext(vm, 88, 100, 50, 50, TestNativeMemUsageChecker{}),
sao: &sparseArrayObject{
items: []sparseArrayItem{
{
idx: 1,
value: New()._newString(newStringValue("key0"), nil),
value: vm.ToValue("key0"),
},
{
idx: 2,
value: New()._newString(newStringValue("key1"), nil),
value: vm.ToValue("key1"),
},
{
idx: 3,
value: New()._newString(newStringValue("key2"), nil),
value: vm.ToValue("key2"),
},
{
idx: 4,
value: New()._newString(newStringValue("key3"), nil),
value: vm.ToValue("key3"),
},
{
idx: 5,
value: New()._newString(newStringValue("key4"), nil),
value: vm.ToValue("key4"),
},
{
idx: 6,
value: New()._newString(newStringValue("key5"), nil),
value: vm.ToValue("key5"),
},
},
},
// array overhead + index size + stringObject (we reach the limit at 4)
expected: SizeEmptyStruct + (SizeInt32+26)*4,
// index size + stringObject (we reach the limit at 4)
newExpected: SizeEmptyStruct + (SizeInt32+58)*4,
// array overhead + index size + (string len + overhead) (we reach the limit at 4)
expected: SizeEmptyStruct + (SizeInt32+(4+SizeString))*4,
// array overhead + index size + (string len + overhead) (we reach the limit at 4)
newExpected: SizeEmptyStruct + (SizeInt32+(4+SizeString))*4,
errExpected: nil,
},
}
Expand Down
28 changes: 12 additions & 16 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func TestArrayObjectMemUsage(t *testing.T) {
name: "mem below threshold given empty slice of values",
mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
ao: &arrayObject{values: []Value{}},
// array overhead + array baseObject
expectedMem: SizeEmptyStruct + SizeEmptyStruct,
// array overhead + array baseObject + values slice overhead
expectedMem: SizeEmptyStruct + SizeEmptyStruct + SizeEmptySlice,
// array overhead + array baseObject + values slice overhead
expectedNewMem: SizeEmptyStruct + SizeEmptyStruct + SizeEmptySlice,
errExpected: nil,
Expand All @@ -168,24 +168,20 @@ func TestArrayObjectMemUsage(t *testing.T) {
mu: NewMemUsageContext(vm, 88, 100, 50, 50, TestNativeMemUsageChecker{}),
ao: &arrayObject{
values: []Value{
vm._newString(newStringValue("key0"), nil),
vm._newString(newStringValue("key1"), nil),
vm._newString(newStringValue("key2"), nil),
vm._newString(newStringValue("key3"), nil),
vm._newString(newStringValue("key4"), nil),
vm._newString(newStringValue("key5"), nil),
vm.ToValue("key0"),
vm.ToValue("key1"),
vm.ToValue("key2"),
vm.ToValue("key3"),
vm.ToValue("key4"),
vm.ToValue("key5"),
},
},
// array overhead + array baseObject
expectedMem: SizeEmptyStruct + SizeEmptyStruct +
// (stringObject + baseObject + prop "length") * entries (at 4 we reach the limit)
(SizeEmptyStruct+SizeEmptyStruct+6)*4 +
// len("keyN") * entries (at 4 we reach the limit)
4*4,
// array overhead + array baseObject + values slice overhead
expectedMem: SizeEmptyStruct + SizeEmptyStruct + SizeEmptySlice +
// len("keyN") with string overhead * entries (at 4 we reach the limit)
(4+SizeString)*4,
// array overhead + array baseObject + values slice overhead
expectedNewMem: SizeEmptyStruct + SizeEmptyStruct + SizeEmptySlice +
// (stringObject + baseObject + prop "length" with string overhead) * entries (at 4 we reach the limit)
(SizeEmptyStruct+SizeEmptyStruct+(6+SizeString))*4 +
// len("keyN") with string overhead * entries (at 4 we reach the limit)
(4+SizeString)*4,
errExpected: nil,
Expand Down
51 changes: 26 additions & 25 deletions builtin_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func BenchmarkMapDeleteJS(b *testing.B) {
}

func TestMapObjectMemUsage(t *testing.T) {
vm := New()
tests := []struct {
name string
mu *MemUsageContext
Expand All @@ -254,68 +255,68 @@ func TestMapObjectMemUsage(t *testing.T) {
}{
{
name: "mem below threshold",
mu: NewMemUsageContext(New(), 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
mo: &mapObject{
m: &orderedMap{
hashTable: map[uint64]*mapEntry{
1: {
key: New()._newString(newStringValue("key"), nil),
value: New()._newString(newStringValue("value"), nil),
key: vm.ToValue("key"),
value: vm.ToValue("value"),
},
},
},
},
// baseObject + stringObject + len(key) + stringObject + len(key)
expectedMem: SizeEmptyStruct + 22 + 3 + 22 + 5,
// baseObject + stringObject + (len(key) + overhead) + stringObject + (len(key) + overhead)
expectedNewMem: SizeEmptyStruct + 38 + (3 + SizeString) + 38 + (5 + SizeString),
// 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(New(), 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
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(New(), 88, 100, 50, 50, TestNativeMemUsageChecker{}),
mu: NewMemUsageContext(vm, 88, 100, 50, 50, TestNativeMemUsageChecker{}),
mo: &mapObject{
m: &orderedMap{
hashTable: map[uint64]*mapEntry{
1: {
key: New()._newString(newStringValue("key"), nil),
value: New()._newString(newStringValue("value"), nil),
key: vm.ToValue("key"),
value: vm.ToValue("value"),
},
2: {
key: New()._newString(newStringValue("key"), nil),
value: New()._newString(newStringValue("value"), nil),
key: vm.ToValue("key"),
value: vm.ToValue("value"),
},
3: {
key: New()._newString(newStringValue("key"), nil),
value: New()._newString(newStringValue("value"), nil),
key: vm.ToValue("key"),
value: vm.ToValue("value"),
},
4: {
key: New()._newString(newStringValue("key"), nil),
value: New()._newString(newStringValue("value"), nil),
key: vm.ToValue("key"),
value: vm.ToValue("value"),
},
},
},
},
// baseObject
expectedMem: SizeEmptyStruct +
// stringObject + len(key) (we reach the limit after 2)
(22+3)*2 +
// stringObject + len(value) (we reach the limit after 2)
(22+5)*2,
// len(key) + overhead (we reach the limit after 3)
(3+SizeString)*3 +
// len(value) + overhead (we reach the limit after 3)
(5+SizeString)*3,
// baseObject
expectedNewMem: SizeEmptyStruct +
// stringObject + len(key) + overhead (we reach the limit after 2)
(38+(3+SizeString))*2 +
// stringObject + len(value) + overhead (we reach the limit after 2)
(38+(5+SizeString))*2,
// len(key) + overhead (we reach the limit after 3)
(3+SizeString)*3 +
// len(value) + overhead (we reach the limit after 3)
(5+SizeString)*3,
errExpected: nil,
},
}
Expand Down
4 changes: 2 additions & 2 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ func TestDateMemUsage(t *testing.T) {
},
// msec value
expectedMem: SizeNumber +
// baseObject overhead + len("test") + value
SizeEmptyStruct + 4 + SizeInt,
// baseObject overhead + len("test") with string overhead + value
SizeEmptyStruct + (4 + SizeString) + SizeInt,
// msec value
expectedNewMem: SizeNumber +
// baseObject overhead + len("test") with string overhead + value
Expand Down
Loading
Loading