Skip to content

Commit

Permalink
feat: add empty slice overhead for arrayObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabri3l committed Aug 8, 2023
1 parent 9b58219 commit 8ca0545
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,13 @@ func (a *arrayObject) MemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsa
if ctx.ArrayLenExceedsThreshold == nil {
return memUsage, newMemUsage, errArrayLenExceedsThresholdNil
}

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

if ctx.ArrayLenExceedsThreshold(len(a.values)) {
inc, newInc, err := a.estimateMemUsage(ctx)
memUsage += inc
Expand Down
16 changes: 13 additions & 3 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestArrayObjectMemUsage(t *testing.T) {
errExpected error
}{
{
name: "mem below threshold",
name: "mem below threshold given a nil slice of values",
mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}),
ao: &arrayObject{},
// array overhead + array baseObject
Expand All @@ -153,6 +153,16 @@ func TestArrayObjectMemUsage(t *testing.T) {
expectedNewMem: SizeEmptyStruct + SizeEmptyStruct,
errExpected: nil,
},
{
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
expectedNewMem: SizeEmptyStruct + SizeEmptyStruct + SizeEmptySlice,
errExpected: nil,
},
{
name: "mem way above threshold returns first crossing of threshold",
mu: NewMemUsageContext(vm, 88, 100, 50, 50, TestNativeMemUsageChecker{}),
Expand All @@ -172,8 +182,8 @@ func TestArrayObjectMemUsage(t *testing.T) {
(SizeEmptyStruct+SizeEmptyStruct+6)*4 +
// len("keyN") * entries (at 4 we reach the limit)
4*4,
// array overhead + array baseObject
expectedNewMem: SizeEmptyStruct + SizeEmptyStruct +
// 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)
Expand Down
1 change: 1 addition & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const (
SizeInt32 = uint64(unsafe.Sizeof(int32(0)))
SizeInt = uint64(unsafe.Sizeof(int(0)))
SizeEmptyStruct = uint64(unsafe.Sizeof((*baseObject)(nil)))
SizeEmptySlice = uint64(unsafe.Sizeof([]Value{}))
// SizeString allows us to take into account the 16 additional bytes
// for any string type in go including the pointer to the start of
// the string data and the length of the string
Expand Down

0 comments on commit 8ca0545

Please sign in to comment.