Histogram and Timeseries vs gcc-4.9 and -Wsign-compare
authorJim Meyering <meyering@fb.com>
Tue, 6 Jan 2015 18:00:14 +0000 (10:00 -0800)
committerViswanath Sivakumar <viswanath@fb.com>
Tue, 13 Jan 2015 19:01:04 +0000 (11:01 -0800)
Summary:
Address multiple -Werror=sign-compare issues exposed when building
tao with gcc-4.9.
In most of these changes I've changed the type of the index in
a for-loop from int to size_t.  The important thing is to use
an unsigned type when the limit is also unsigned.  I choose size_t
because the general recommendation (when writing portable code)
to avoid size-tied types like uint32_t and uint64_t unless you
have a very good reason to require them.

Test Plan:
Run this and note there are fewer errors than before:
fbconfig --platform-all=gcc-4.9-glibc-2.20 tao/server && fbmake dbgo

Reviewed By: andrei.alexandrescu@fb.com

Subscribers: trunkagent, net-systems@, folly-diffs@

FB internal diff: D1766651

Tasks: 5941250

Signature: t1:1766651:1420594537:56ef53ca233e1649469db9570942c1d5dd47cf6d

folly/stats/Histogram.h
folly/stats/MultiLevelTimeSeries-defs.h
folly/stats/TimeseriesHistogram-defs.h
folly/stats/TimeseriesHistogram.h

index cf49be7c58f5f42f1b1ea2e87d09e3e486dd4ffa..0276c97e19433f85eb47248c99df300195e6a4a9 100644 (file)
@@ -280,7 +280,7 @@ class Histogram {
 
   /* Remove all data points from the histogram */
   void clear() {
-    for (int i = 0; i < buckets_.getNumBuckets(); i++) {
+    for (unsigned int i = 0; i < buckets_.getNumBuckets(); i++) {
       buckets_.getByIndex(i).clear();
     }
   }
@@ -296,7 +296,7 @@ class Histogram {
       throw std::invalid_argument("Cannot subtract input histogram.");
     }
 
-    for (int i = 0; i < buckets_.getNumBuckets(); i++) {
+    for (unsigned int i = 0; i < buckets_.getNumBuckets(); i++) {
       buckets_.getByIndex(i) -= hist.buckets_.getByIndex(i);
     }
   }
@@ -312,7 +312,7 @@ class Histogram {
       throw std::invalid_argument("Cannot merge from input histogram.");
     }
 
-    for (int i = 0; i < buckets_.getNumBuckets(); i++) {
+    for (unsigned int i = 0; i < buckets_.getNumBuckets(); i++) {
       buckets_.getByIndex(i) += hist.buckets_.getByIndex(i);
     }
   }
@@ -327,7 +327,7 @@ class Histogram {
       throw std::invalid_argument("Cannot copy from input histogram.");
     }
 
-    for (int i = 0; i < buckets_.getNumBuckets(); i++) {
+    for (unsigned int i = 0; i < buckets_.getNumBuckets(); i++) {
       buckets_.getByIndex(i) = hist.buckets_.getByIndex(i);
     }
   }
index cb119d8036691b048d609d3eada08867e5ac240c..60c291cf21081c9f0fd6333717989478be06d332 100644 (file)
@@ -33,7 +33,7 @@ MultiLevelTimeSeries<VT, TT>::MultiLevelTimeSeries(
     CHECK(levelDurations);
 
     levels_.reserve(nLevels);
-    for (int i = 0; i < nLevels; ++i) {
+    for (size_t i = 0; i < nLevels; ++i) {
       if (levelDurations[i] == TT(0)) {
         CHECK_EQ(i, nLevels - 1);
       } else if (i > 0) {
@@ -71,7 +71,7 @@ void MultiLevelTimeSeries<VT, TT>::addValueAggregated(TimeType now,
 template <typename VT, typename TT>
 void MultiLevelTimeSeries<VT, TT>::update(TimeType now) {
   flush();
-  for (int i = 0; i < levels_.size(); ++i) {
+  for (size_t i = 0; i < levels_.size(); ++i) {
     levels_[i].update(now);
   }
 }
@@ -80,7 +80,7 @@ template <typename VT, typename TT>
 void MultiLevelTimeSeries<VT, TT>::flush() {
   // update all the underlying levels
   if (cachedCount_ > 0) {
-    for (int i = 0; i < levels_.size(); ++i) {
+    for (size_t i = 0; i < levels_.size(); ++i) {
       levels_[i].addValueAggregated(cachedTime_, cachedSum_, cachedCount_);
     }
     cachedCount_ = 0;
index 094cdde7a2e42b96736e48845a14abb52feb0040..b9edeaf37371d994d7af920f71013790b22c35bb 100644 (file)
@@ -29,7 +29,7 @@ template <typename ReturnType>
 ReturnType TimeseriesHistogram<T, TT, C>::avg(int level) const {
   ValueType total = ValueType();
   int64_t nsamples = 0;
-  for (int b = 0; b < buckets_.getNumBuckets(); ++b) {
+  for (unsigned int b = 0; b < buckets_.getNumBuckets(); ++b) {
     const auto& levelObj = buckets_.getByIndex(b).getLevel(level);
     total += levelObj.sum();
     nsamples += levelObj.count();
@@ -43,7 +43,7 @@ ReturnType TimeseriesHistogram<T, TT, C>::avg(TimeType start,
                                               TimeType end) const {
   ValueType total = ValueType();
   int64_t nsamples = 0;
-  for (int b = 0; b < buckets_.getNumBuckets(); ++b) {
+  for (unsigned int b = 0; b < buckets_.getNumBuckets(); ++b) {
     const auto& levelObj = buckets_.getByIndex(b).getLevel(start, end);
     total += levelObj.sum(start, end);
     nsamples += levelObj.count(start, end);
@@ -57,7 +57,7 @@ ReturnType TimeseriesHistogram<T, TT, C>::rate(TimeType start,
                                                TimeType end) const {
   ValueType total = ValueType();
   TimeType elapsed(0);
-  for (int b = 0; b < buckets_.getNumBuckets(); ++b) {
+  for (unsigned int b = 0; b < buckets_.getNumBuckets(); ++b) {
     const auto& level = buckets_.getByIndex(b).getLevel(start);
     total += level.sum(start, end);
     elapsed = std::max(elapsed, level.elapsed(start, end));
@@ -169,7 +169,7 @@ template <typename T, typename TT, typename C>
 T TimeseriesHistogram<T, TT, C>::rate(int level) const {
   ValueType total = ValueType();
   TimeType elapsed(0);
-  for (int b = 0; b < buckets_.getNumBuckets(); ++b) {
+  for (unsigned int b = 0; b < buckets_.getNumBuckets(); ++b) {
     const auto& levelObj = buckets_.getByIndex(b).getLevel(level);
     total += levelObj.sum();
     elapsed = std::max(elapsed, levelObj.elapsed());
index 7f4bc0841d12b8c3c47bc10dd84ca84e75d4b6ac..6c944fed4203698e76c7707f5a7b28711cf8eb7d 100644 (file)
@@ -118,7 +118,7 @@ class TimeseriesHistogram {
   /* Total count of values at the given timeseries level (all buckets). */
   int64_t count(int level) const {
     int64_t total = 0;
-    for (int b = 0; b < buckets_.getNumBuckets(); ++b) {
+    for (unsigned int b = 0; b < buckets_.getNumBuckets(); ++b) {
       total += buckets_.getByIndex(b).count(level);
     }
     return total;
@@ -127,7 +127,7 @@ class TimeseriesHistogram {
   /* Total count of values added during the given interval (all buckets). */
   int64_t count(TimeType start, TimeType end) const {
     int64_t total = 0;
-    for (int b = 0; b < buckets_.getNumBuckets(); ++b) {
+    for (unsigned int b = 0; b < buckets_.getNumBuckets(); ++b) {
       total += buckets_.getByIndex(b).count(start, end);
     }
     return total;
@@ -136,7 +136,7 @@ class TimeseriesHistogram {
   /* Total sum of values at the given timeseries level (all buckets). */
   ValueType sum(int level) const {
     ValueType total = ValueType();
-    for (int b = 0; b < buckets_.getNumBuckets(); ++b) {
+    for (unsigned int b = 0; b < buckets_.getNumBuckets(); ++b) {
       total += buckets_.getByIndex(b).sum(level);
     }
     return total;
@@ -145,7 +145,7 @@ class TimeseriesHistogram {
   /* Total sum of values added during the given interval (all buckets). */
   ValueType sum(TimeType start, TimeType end) const {
     ValueType total = ValueType();
-    for (int b = 0; b < buckets_.getNumBuckets(); ++b) {
+    for (unsigned int b = 0; b < buckets_.getNumBuckets(); ++b) {
       total += buckets_.getByIndex(b).sum(start, end);
     }
     return total;