Skip to content

Don't call result.ToString() in Random.AssertInRange - #52918

Merged
jkotas merged 4 commits into
dotnet:mainfrom
EgorBo:checked-random-opt
May 18, 2021
Merged

jkotas merged 4 commits into
dotnet:mainfrom
EgorBo:checked-random-opt

Conversation

@EgorBo

@EgorBo EgorBo commented May 18, 2021

Copy link
Copy Markdown
Member

Fixes #52894

Double.ToString() is quite slow in Checked (partially because of get_CurrentCulture calls + in general I guess Grisu is full of asserts)

ghost commented May 18, 2021

Copy link
Copy Markdown

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@zlatanov

ghost commented May 18, 2021

Copy link
Copy Markdown
Contributor

Would it make sense to update the other assert method to do the same?

[Conditional("DEBUG")]
private static void AssertInRange(long result, long minInclusive, long maxExclusive)
{
if (maxExclusive > minInclusive)
{
Debug.Assert(result >= minInclusive && result < maxExclusive, $"Expected {minInclusive} <= {result} < {maxExclusive}");
}
else
{
Debug.Assert(result == minInclusive, $"Expected {minInclusive} == {result}");
}
}

@EgorBo

ghost commented May 18, 2021 •

Copy link
Copy Markdown
Member Author

Would it make sense to update the other assert method to do the same?

[Conditional("DEBUG")]
private static void AssertInRange(long result, long minInclusive, long maxExclusive)
{
if (maxExclusive > minInclusive)
{
Debug.Assert(result >= minInclusive && result < maxExclusive, $"Expected {minInclusive} <= {result} < {maxExclusive}");
}
else
{
Debug.Assert(result == minInclusive, $"Expected {minInclusive} == {result}");
}
}

long to string is way faster but it's probably still makes sense yeah

@SingleAccretion

ghost commented May 18, 2021

Copy link
Copy Markdown
Contributor

Heh, I suppose this would be a case where a custom string interpolation builder as the argument would help.

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM - thanks!

@jkotas
jkotas merged commit d93d531 into dotnet:main May 18, 2021
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Random.Next is slow on checked runtime on ARM

6 participants