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

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: fixed signed/unsigned comparison 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..6c4815e5186d04b37325a34984902ffe9b58cf54 100644
--- a/base/metrics/statistics_recorder.h
+++ b/base/metrics/statistics_recorder.h
@@ -31,8 +31,35 @@ class BucketRanges;
class Lock;
class BASE_EXPORT StatisticsRecorder {
+ // A class used as a key for the histogram map below. It always references
Alexei Svitkine (slow) 2016/04/07 20:21:10 Nit: Move this within the public section below.
bcwhite 2016/04/07 20:24:33 Oh? It's not something that should be accessed ou
Alexei Svitkine (slow) 2016/04/07 20:49:08 From the style guide: "Use the specified order of
bcwhite 2016/04/07 21:09:16 It does say "should". I think that would allow fo
+ // 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.
+ return wordmemcmp(data(), rhs.data(), length()) < 0;
Alexei Svitkine (slow) 2016/04/07 20:21:10 Nit: How about "return StringPiece::operator<(rhs)
bcwhite 2016/04/07 20:24:33 I had something like that previously but removed i
Alexei Svitkine (slow) 2016/04/07 20:49:08 I see - I guess since we don't if the compiler wil
bcwhite 2016/04/07 21:09:16 Done.
+ }
+ };
+
public:
- typedef std::map<uint64_t, HistogramBase*> HistogramMap; // Key is name-hash.
+ 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