Bug 806514 - Replaced std::vector with nsTArray in Histogram::SampleSet. r?chutten draft
authorRofael Aleezada <me@rofael.com>
Wed, 23 May 2018 21:50:29 -0500
changeset 800189 7cc866a53961db5ffcc93236da94d02e16147c4a
parent 800025 94d7f0e1c4d0390450028972cfeb65e0550b9892
push id111296
push userbmo:me@rofael.com
push dateSat, 26 May 2018 05:49:30 +0000
reviewerschutten
bugs806514
milestone62.0a1
Bug 806514 - Replaced std::vector with nsTArray in Histogram::SampleSet. r?chutten MozReview-Commit-ID: 6ptwiOz2c5i
ipc/chromium/src/base/histogram.cc
ipc/chromium/src/base/histogram.h
toolkit/components/telemetry/TelemetryHistogram.cpp
--- a/ipc/chromium/src/base/histogram.cc
+++ b/ipc/chromium/src/base/histogram.cc
@@ -232,19 +232,17 @@ size_t Histogram::SizeOfIncludingThis(mo
   n += aMallocSizeOf(this);
   n += sample_.SizeOfExcludingThis(aMallocSizeOf);
   return n;
 }
 
 size_t
 Histogram::SampleSet::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf)
 {
-  // We're not allowed to do deep dives into STL data structures.  This
-  // is as close as we can get to measuring this array.
-  return aMallocSizeOf(&counts_[0]);
+  return counts_.ShallowSizeOfExcludingThis(aMallocSizeOf);
 }
 
 Histogram::Histogram(Sample minimum, Sample maximum, size_t bucket_count)
   : sample_(),
     declared_min_(minimum),
     declared_max_(maximum),
     bucket_count_(bucket_count),
     flags_(kNoFlags),
@@ -424,17 +422,20 @@ Histogram::SampleSet::SampleSet()
       sum_(0),
       redundant_count_(0) {
 }
 
 Histogram::SampleSet::~SampleSet() {
 }
 
 void Histogram::SampleSet::Resize(const Histogram& histogram) {
-  counts_.resize(histogram.bucket_count(), 0);
+  size_t oldSize = counts_.Length();
+  counts_.SetLength(histogram.bucket_count());
+
+  for (size_t i = oldSize; i < histogram.bucket_count(); ++i) counts_[i] = 0;
 }
 
 void Histogram::SampleSet::Accumulate(Sample value, Count count,
                                       size_t index) {
   DCHECK(count == 1 || count == -1);
   counts_[index] += count;
   redundant_count_ += count;
   sum_ += static_cast<int64_t>(count) * value;
@@ -449,20 +450,20 @@ Count Histogram::SampleSet::TotalCount()
        it != counts_.end();
        ++it) {
     total += *it;
   }
   return total;
 }
 
 void Histogram::SampleSet::Add(const SampleSet& other) {
-  DCHECK_EQ(counts_.size(), other.counts_.size());
+  DCHECK_EQ(counts_.Length(), other.counts_.Length());
   sum_ += other.sum_;
   redundant_count_ += other.redundant_count_;
-  for (size_t index = 0; index < counts_.size(); ++index)
+  for (size_t index = 0; index < counts_.Length(); ++index)
     counts_[index] += other.counts_[index];
 }
 
 //------------------------------------------------------------------------------
 // LinearHistogram: This histogram uses a traditional set of evenly spaced
 // buckets.
 //------------------------------------------------------------------------------
 
--- a/ipc/chromium/src/base/histogram.h
+++ b/ipc/chromium/src/base/histogram.h
@@ -43,21 +43,22 @@
 #define BASE_METRICS_HISTOGRAM_H_
 #pragma once
 
 #include "mozilla/Atomics.h"
 #include "mozilla/MemoryReporting.h"
 
 #include <map>
 #include <string>
-#include <vector>
 
 #include "base/time.h"
 #include "base/lock.h"
 
+#include "nsTArray.h"
+
 namespace base {
 
 //------------------------------------------------------------------------------
 
 class BooleanHistogram;
 class CustomHistogram;
 class Histogram;
 class LinearHistogram;
@@ -65,17 +66,17 @@ class LinearHistogram;
 class Histogram {
  public:
   typedef int Sample;  // Used for samples (and ranges of samples).
   typedef int Count;  // Used to count samples in a bucket.
   static const Sample kSampleType_MAX = INT_MAX;
   // Initialize maximum number of buckets in histograms as 16,384.
   static const size_t kBucketCount_MAX;
 
-  typedef std::vector<Count> Counts;
+  typedef nsTArray<Count> Counts;
   typedef const Sample* Ranges;
 
   // These enums are used to facilitate deserialization of renderer histograms
   // into the browser.
   enum ClassType {
     HISTOGRAM,
     LINEAR_HISTOGRAM,
     BOOLEAN_HISTOGRAM,
@@ -147,17 +148,17 @@ class Histogram {
     Count TotalCount() const;
     int64_t sum() const {
        return sum_;
     }
     int64_t redundant_count() const {
        return redundant_count_;
     }
     size_t size() const {
-       return counts_.size();
+       return counts_.Length();
     }
 
    protected:
     // Actual histogram data is stored in buckets, showing the count of values
     // that fit into each bucket.
     Counts counts_;
 
     // Save simple stats locally.  Note that this MIGHT get done in base class
--- a/toolkit/components/telemetry/TelemetryHistogram.cpp
+++ b/toolkit/components/telemetry/TelemetryHistogram.cpp
@@ -2532,17 +2532,18 @@ public:
 };
 
 PersistedSampleSet::PersistedSampleSet(const nsTArray<Histogram::Count>& aCounts,
                                        int64_t aSampleSum)
 {
   // Initialize the data in the base class. See Histogram::SampleSet
   // for the fields documentation.
   const size_t numCounts = aCounts.Length();
-  counts_.resize(numCounts);
+  counts_.SetLength(numCounts);
+
   for (size_t i = 0; i < numCounts; i++) {
     counts_[i] = aCounts[i];
     redundant_count_ += aCounts[i];
   }
   sum_ = aSampleSum;
 };
 } // base (from ipc/chromium/src/base)