Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1838)

Unified Diff: base/metrics/statistics_recorder.h

Issue 1866423002: Use strings for keys in the histogram map. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed review comments by Alexei Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/metrics/statistics_recorder.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/metrics/statistics_recorder.h
diff --git a/base/metrics/statistics_recorder.h b/base/metrics/statistics_recorder.h
index 6eaf07959f127f9821d3f077360bf22db20bcec0..12543b8d91e35b3b98dbf609195caefbcb0759d6 100644
--- a/base/metrics/statistics_recorder.h
+++ b/base/metrics/statistics_recorder.h
@@ -32,7 +32,36 @@ class Lock;
class BASE_EXPORT StatisticsRecorder {
public:
- typedef std::map<uint64_t, HistogramBase*> HistogramMap; // Key is name-hash.
+ // A class used as a key for the histogram map below. It always references
+ // a string owned outside of this class, likely in the value of the map.
+ class StringKey : public StringPiece {
+ public:
+ // Constructs the StringKey using various sources. The source must live
+ // at least as long as the created object.
+ StringKey(const std::string& str) : StringPiece(str) {}
+ StringKey(StringPiece str) : StringPiece(str) {}
+
+ // Though StringPiece is better passed by value than by reference, in
+ // this case it's being passed many times and likely already been stored
+ // in memory (not just registers) so the benefit of pass-by-value is
+ // negated.
+ bool operator<(const StringKey& rhs) const {
+ // Since order is unimportant in the map and string comparisons can be
+ // slow, use the length as the primary sort value.
+ if (length() < rhs.length())
+ return true;
+ if (length() > rhs.length())
+ return false;
+
+ // Fall back to an actual string comparison. The lengths are the same
+ // so a simple memory-compare is sufficient. This is slightly more
+ // efficient than calling operator<() for StringPiece which would
+ // again have to check lengths before calling wordmemcmp().
+ return wordmemcmp(data(), rhs.data(), length()) < 0;
+ }
+ };
+
+ typedef std::map<StringKey, HistogramBase*> HistogramMap;
typedef std::vector<HistogramBase*> Histograms;
// A class for iterating over the histograms held within this global resource.
« no previous file with comments | « no previous file | base/metrics/statistics_recorder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698