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-23396: add strings overhead to mem usage calculations #86

Merged
merged 29 commits into from
Aug 8, 2023

Conversation

Gabri3l
Copy link

@Gabri3l Gabri3l commented Jul 24, 2023

This is a small ma pretty important change. Strings in go have an overhead of 16bytes, currently an empty string adds up to 0 bytes from the way we calculate memory usage which is not correct. With this change every native string or object key (which are also strings) will also account for the overhead defined in SizeString.

EDIT

To understand the impact of this change all MemUsage functions return the existing value + an adjusted one. This will help us understand the impact of such a change before we roll it out to customers. I split the commit logically for easier review but beware that the whole test suite only passes with the last commits.

  • Original commit that has been already reviewed
  • Update the interfaces to the new MemUsage signature
  • Update every single MemUsage function to use the new signature + add tests
  • A small refactor based on Nick's comment

Generally I noticed a few missing nil checks and other incorrect MemUsage functions, the diff should be easy to spot from the single commits.

Copy link

@LijieZhang1998 LijieZhang1998 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a wonderful find! I only have one question but not really related to your change.

memory_test.go Outdated Show resolved Hide resolved
object.go Outdated Show resolved Hide resolved
memory_test.go Outdated Show resolved Hide resolved
Copy link

@jwongmongodb jwongmongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! nothing else to add on my side. Great find!

Copy link

@kpatel71716 kpatel71716 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🤑

Copy link

@LijieZhang1998 LijieZhang1998 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🥇 I just have one question about the MemUsageLimitExceeded check.

array.go Outdated
}
if exceeded := ctx.MemUsageLimitExceeded(total); exceeded {
return total, nil
if exceeded := ctx.MemUsageLimitExceeded(memUsage); exceeded {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] should we use mewMemUsage instead of memUsage?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, eventually the newMem and mem will have the same value but we are using this PR to collect data about the gap between the two values for customers to get an idea of the impact of this change. Until we apply this new changes all the memory logic will rely on the current memUsage.

value.go Outdated Show resolved Hide resolved
Copy link

@nickpoindexter nickpoindexter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just had some questions and optional things.

array.go Outdated Show resolved Hide resolved
builtin_map.go Show resolved Hide resolved
array_test.go Show resolved Hide resolved
date.go Show resolved Hide resolved
mem_context.go Show resolved Hide resolved
string_ascii_test.go Outdated Show resolved Hide resolved
string_imported_test.go Outdated Show resolved Hide resolved
{
name: "should have a value given the length of the string",
val: unicodeString{' ', 'y', 'o'},
expected: 2,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] why 2 instead of 3? Does space not count?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this gets converted to a string (see the String() method on this type) we skip the first item which can represent the little-indian/big-indian convention but not the actual content of the string. Not 100% that's what the first char is used for but I know that even comparisons between strings skip the fist char if it's a unicode string.

Copy link

@jwongmongodb jwongmongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

string_test.go Outdated Show resolved Hide resolved
builtin_map.go Outdated Show resolved Hide resolved
runtime.go Outdated Show resolved Hide resolved
Copy link

@nickpoindexter nickpoindexter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Will be interesting to see the results for customers

Copy link

@jwongmongodb jwongmongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

nv = new MyNativeVal()
checkMem();`,
testNativeValueMemUsage + 2,
expectedSizeDiff: testNativeValueMemUsage +
2, // "nv"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] what does nv mean here? Native Value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for the variable name nv, so it's the len("nv")

Copy link

@kpatel71716 kpatel71716 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Gabri3l Gabri3l merged commit 1e62d92 into mongodb-forks:realm Aug 8, 2023
0 of 4 checks passed
@Gabri3l Gabri3l deleted the BAAS-23396 branch August 8, 2023 19:29
Gabri3l added a commit that referenced this pull request Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants