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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | base/metrics/statistics_recorder.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // StatisticsRecorder holds all Histograms and BucketRanges that are used by 5 // StatisticsRecorder holds all Histograms and BucketRanges that are used by
6 // Histograms in the system. It provides a general place for 6 // Histograms in the system. It provides a general place for
7 // Histograms/BucketRanges to register, and supports a global API for accessing 7 // Histograms/BucketRanges to register, and supports a global API for accessing
8 // (i.e., dumping, or graphing) the data. 8 // (i.e., dumping, or graphing) the data.
9 9
10 #ifndef BASE_METRICS_STATISTICS_RECORDER_H_ 10 #ifndef BASE_METRICS_STATISTICS_RECORDER_H_
(...skipping 14 matching lines...) Expand all
25 #include "base/metrics/histogram_base.h" 25 #include "base/metrics/histogram_base.h"
26 #include "base/strings/string_piece.h" 26 #include "base/strings/string_piece.h"
27 27
28 namespace base { 28 namespace base {
29 29
30 class BucketRanges; 30 class BucketRanges;
31 class Lock; 31 class Lock;
32 32
33 class BASE_EXPORT StatisticsRecorder { 33 class BASE_EXPORT StatisticsRecorder {
34 public: 34 public:
35 typedef std::map<uint64_t, HistogramBase*> HistogramMap; // Key is name-hash. 35 // A class used as a key for the histogram map below. It always references
36 // a string owned outside of this class, likely in the value of the map.
37 class StringKey : public StringPiece {
38 public:
39 // Constructs the StringKey using various sources. The source must live
40 // at least as long as the created object.
41 StringKey(const std::string& str) : StringPiece(str) {}
42 StringKey(StringPiece str) : StringPiece(str) {}
43
44 // Though StringPiece is better passed by value than by reference, in
45 // this case it's being passed many times and likely already been stored
46 // in memory (not just registers) so the benefit of pass-by-value is
47 // negated.
48 bool operator<(const StringKey& rhs) const {
49 // Since order is unimportant in the map and string comparisons can be
50 // slow, use the length as the primary sort value.
51 if (length() < rhs.length())
52 return true;
53 if (length() > rhs.length())
54 return false;
55
56 // Fall back to an actual string comparison. The lengths are the same
57 // so a simple memory-compare is sufficient. This is slightly more
58 // efficient than calling operator<() for StringPiece which would
59 // again have to check lengths before calling wordmemcmp().
60 return wordmemcmp(data(), rhs.data(), length()) < 0;
61 }
62 };
63
64 typedef std::map<StringKey, HistogramBase*> HistogramMap;
36 typedef std::vector<HistogramBase*> Histograms; 65 typedef std::vector<HistogramBase*> Histograms;
37 66
38 // A class for iterating over the histograms held within this global resource. 67 // A class for iterating over the histograms held within this global resource.
39 class BASE_EXPORT HistogramIterator { 68 class BASE_EXPORT HistogramIterator {
40 public: 69 public:
41 HistogramIterator(const HistogramMap::iterator& iter, 70 HistogramIterator(const HistogramMap::iterator& iter,
42 bool include_persistent); 71 bool include_persistent);
43 HistogramIterator(const HistogramIterator& rhs); // Must be copyable. 72 HistogramIterator(const HistogramIterator& rhs); // Must be copyable.
44 ~HistogramIterator(); 73 ~HistogramIterator();
45 74
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 208
180 // Lock protects access to above maps. 209 // Lock protects access to above maps.
181 static base::Lock* lock_; 210 static base::Lock* lock_;
182 211
183 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); 212 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder);
184 }; 213 };
185 214
186 } // namespace base 215 } // namespace base
187 216
188 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ 217 #endif // BASE_METRICS_STATISTICS_RECORDER_H_
OLDNEW
« 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