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

A bug in Histogram? #2006

Open
JJLovesLife opened this issue Mar 10, 2024 · 0 comments
Open

A bug in Histogram? #2006

JJLovesLife opened this issue Mar 10, 2024 · 0 comments

Comments

@JJLovesLife
Copy link

I was looking around the source code and the following piece of code seems a bit buggy to me:

public void AddMetric(float metric, int bucket)
{
Debug.Assert(0 <= bucket && bucket < Count);
if (m_singleBucketNum < 0)
{
m_singleBucketNum = bucket;
}
if (m_singleBucketNum == bucket)
{
m_singleBucketValue += metric;
return;
}
if (m_buckets == null)
{
m_buckets = new float[Count];
m_buckets[m_singleBucketNum] = m_singleBucketValue;
}
m_buckets[bucket] += metric;
}

Let's say the following sequence of AddMetric is called:

AddMetric(1.0f, 0);
AddMetric(2.0f, 1);
AddMetric(3.0f, 0);

The second time will cause the array to be constructed, and m_singleBucketValue is populated into the array.
But within the third call, if (m_singleBucketNum == bucket) will still be true, and the metric is still added into m_singleBucketValue instead of the array.
Inside the indexer:

if (m_buckets != null)
{
return m_buckets[index];
}
else if (m_singleBucketNum == index)
{
return m_singleBucketValue;
}

the m_singleBucketValue isn't used when the array is not null, causing the entry for m_singleBucketNum to be returned with incorrected value.

I don't have the whole knowledge of how Histogram is used, so I don't sure if this is really a bug. And this problem might not be a big issue due to the usage pattern, cause I don't see any bucket to be obviously wrong in the GUI.

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

No branches or pull requests

1 participant