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 rounding error in getLikelyClassesOrMethods #86965

Merged
merged 6 commits into from Jun 1, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/coreclr/jit/likelyclass.cpp
Expand Up @@ -244,12 +244,27 @@ static unsigned getLikelyClassesOrMethods(LikelyClassMethodRecord*

const UINT32 numberOfClasses = min(knownHandles, maxLikelyClasses);

double roundingError = 0.0;
for (size_t hIdx = 0; hIdx < numberOfClasses; hIdx++)
{
LikelyClassMethodHistogramEntry const hc = sortedEntries[hIdx];
pLikelyEntries[hIdx].handle = hc.m_handle;
pLikelyEntries[hIdx].likelihood = hc.m_count * 100 / h.m_totalCount;
LikelyClassMethodHistogramEntry const hc = sortedEntries[hIdx];
const double likelihood = hc.m_count * 100.0 / h.m_totalCount;
pLikelyEntries[hIdx].handle = hc.m_handle;
pLikelyEntries[hIdx].likelihood = (UINT32)likelihood;

// Accumulate the rounding error
roundingError += likelihood - (double)(UINT32)likelihood;
}

// Distribute the rounding error, just apply it to the first entry
// so we can avoid marking fallback case as e.g. 1% likely while in fact it's 0.
if (roundingError >= 1.0)
{
assert(numberOfClasses > 0);
pLikelyEntries[0].likelihood += (UINT32)roundingError;
assert(pLikelyEntries[0].likelihood <= 100);
}

EgorBo marked this conversation as resolved.
Show resolved Hide resolved
return numberOfClasses;
}
}
Expand Down