From: Jim Meyering Date: Tue, 6 Jan 2015 18:00:14 +0000 (-0800) Subject: Histogram and Timeseries vs gcc-4.9 and -Wsign-compare X-Git-Tag: v0.22.0~26 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=29be5d6dca2f048f194d890c8e6c90a999fd3ba4;p=folly.git Histogram and Timeseries vs gcc-4.9 and -Wsign-compare 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 --- diff --git a/folly/stats/Histogram.h b/folly/stats/Histogram.h index cf49be7c..0276c97e 100644 --- a/folly/stats/Histogram.h +++ b/folly/stats/Histogram.h @@ -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); } } diff --git a/folly/stats/MultiLevelTimeSeries-defs.h b/folly/stats/MultiLevelTimeSeries-defs.h index cb119d80..60c291cf 100644 --- a/folly/stats/MultiLevelTimeSeries-defs.h +++ b/folly/stats/MultiLevelTimeSeries-defs.h @@ -33,7 +33,7 @@ MultiLevelTimeSeries::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::addValueAggregated(TimeType now, template void MultiLevelTimeSeries::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 void MultiLevelTimeSeries::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; diff --git a/folly/stats/TimeseriesHistogram-defs.h b/folly/stats/TimeseriesHistogram-defs.h index 094cdde7..b9edeaf3 100644 --- a/folly/stats/TimeseriesHistogram-defs.h +++ b/folly/stats/TimeseriesHistogram-defs.h @@ -29,7 +29,7 @@ template ReturnType TimeseriesHistogram::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::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::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 T TimeseriesHistogram::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()); diff --git a/folly/stats/TimeseriesHistogram.h b/folly/stats/TimeseriesHistogram.h index 7f4bc084..6c944fed 100644 --- a/folly/stats/TimeseriesHistogram.h +++ b/folly/stats/TimeseriesHistogram.h @@ -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;