Skip to content
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
15 changes: 13 additions & 2 deletions src/librustc/util/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,27 @@ macro_rules! define_categories {
$(
let (hits, total) = self.query_counts.$name;

//normalize hits to 0%
let hit_percent =
if total > 0 {
((hits as f32) / (total as f32)) * 100.0
} else {
0.0
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use Laplace smoothing instead - (1 + hits) / (2 + total).
It's really simple, but still useful when you want to count almost any kind of interesting/all fractions.

Copy link
Contributor

Choose a reason for hiding this comment

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

If not, then r=me

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm intrigued but I don't think I completely understand the idea. In the case where the category has no data hits=0 and total=0 so wouldn't this result in (1 + 0) / (2 + 0) => 1/2 => 0.5 aka 50% cache hits when there were no hits or attempts at all?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, if there's zero prior knowledge then the cache hit probability is 50% - it's either hit or not :)

If you have some better prior knowledge than that, then you can tweak the α parameter in (α + hits)/(2α + total).
Either way you won't have to divide by zero.

Copy link
Member Author

Choose a reason for hiding this comment

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

It just seems like a bug to me that if there's no data, cache hit ratio would be 50%. If it's ok with you, I'd prefer to leave the code as is. :)

};

json.push_str(&format!(
"{{ \"category\": {}, \"time_ms\": {},
\"query_count\": {}, \"query_hits\": {} }}",
\"query_count\": {}, \"query_hits\": {} }},",
stringify!($name),
self.times.$name / 1_000_000,
total,
format!("{:.2}", (((hits as f32) / (total as f32)) * 100.0))
format!("{:.2}", hit_percent)
));
)*

//remove the trailing ',' character
json.pop();

json.push(']');

json
Expand Down