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

Fix VeryLargeAmountOfEnumsToSerialize test #37710

Merged
merged 2 commits into from
Jun 10, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,36 @@ public static void MoreThan64EnumValuesToSerialize()
public static void VeryLargeAmountOfEnumsToSerialize()
{
// Ensure we don't throw OutOfMemoryException.
// Multiple threads are used to ensure the approximate size limit
// for the name cache(a concurrent dictionary) is honored.

var options = new JsonSerializerOptions
{
Converters = { new JsonStringEnumConverter() }
};

const int MaxValue = 33554432; // Value for MyEnum.Z
Task[] tasks = new Task[MaxValue];
const int MaxValue = 2097152; // value for MyEnum.V

for (int i = 0; i < tasks.Length; i++)
// Every value between 0 and MaxValue maps to a valid enum
// identifier, and is a candidate to go into the name cache.

// Write the first 45 values.
for (int i = 1; i < 46; i++)
{
tasks[i] = Task.Run(() => JsonSerializer.Serialize((MyEnum)i, options));
JsonSerializer.Serialize((MyEnum)i, options);
}

Task.WaitAll(tasks);
// At this point, there are 60 values in the name cache;
// 22 cached at warm-up, the rest in the above loop.

// Ensure the approximate size limit for the name cache (a concurrent dictionary) is honored.
// Use multiple threads to perhaps go over the soft limit of 64, but not by more than a couple.
Parallel.For(0, 8, i => JsonSerializer.Serialize((MyEnum)(46 + i), options));

// Write the remaining enum values. We should not store any more values in
// the cache. If we do, we may throw OutOfMemoryException on some machines.
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps clarify a bit that the OutOfMemoryException would be thrown if we didn't have a cap on the dictionary.

            // Write the remaining enum values. The cache is capped to avoid OutOfMemoryException due
            // to having too many cached items.

Copy link
Contributor Author

@layomia layomia Jun 10, 2020

Choose a reason for hiding this comment

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

Will update the comment in #37720 (to avoid CI reset).

for (int i = 54; i <= MaxValue; i++)
Copy link
Member

Choose a reason for hiding this comment

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

How long does this test take?

Copy link
Contributor Author

@layomia layomia Jun 10, 2020

Choose a reason for hiding this comment

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

It takes ~6.75s on my machine (which would be ~20% of total test time if it were not outer-loop).

{
JsonSerializer.Serialize((MyEnum)i, options);
}
}

[Flags]
Expand Down Expand Up @@ -281,10 +294,6 @@ public enum MyEnum
T = 1 << 19,
U = 1 << 20,
V = 1 << 21,
W = 1 << 22,
X = 1 << 23,
Y = 1 << 24,
Z = 1 << 25,
}
}
}