Commit 84d98c46 authored by Sergey Zhupanov's avatar Sergey Zhupanov Committed by Facebook Github Bot

Minor folly cleanup.

Summary: Minor refactoring of several fbcode/folly/ classes.  Primarily constification, replacing custom code with call to boost, etc.

Reviewed By: yfeldblum

Differential Revision: D8239891

fbshipit-source-id: 14cb109b5deb27f8949b7889d195fcba29e2038c
parent 8dfae42b
......@@ -65,7 +65,7 @@ class ThreadCachedInt : boost::noncopyable {
IntT readFull() const {
// This could race with thread destruction and so the access lock should be
// acquired before reading the current value
auto accessor = cache_.accessAllThreads();
const auto accessor = cache_.accessAllThreads();
IntT ret = readFast();
for (const auto& cache : accessor) {
if (!cache.reset_.load(std::memory_order_acquire)) {
......
......@@ -69,7 +69,7 @@ class ThreadLocal {
: constructor_(std::forward<F>(constructor)) {}
T* get() const {
T* ptr = tlp_.get();
T* const ptr = tlp_.get();
if (LIKELY(ptr != nullptr)) {
return ptr;
}
......@@ -105,7 +105,7 @@ class ThreadLocal {
ThreadLocal& operator=(const ThreadLocal&) = delete;
T* makeTlp() const {
auto ptr = constructor_();
auto const ptr = constructor_();
tlp_.reset(ptr);
return ptr;
}
......@@ -274,7 +274,7 @@ class ThreadLocalPtr {
e_ = e_->prev;
decrementToValid();
}
const T& dereference() const {
return *static_cast<T*>(e_->elements[accessor_->id_].ptr);
}
......
......@@ -20,27 +20,24 @@
namespace folly {
TimeoutQueue::Id TimeoutQueue::add(
int64_t now,
int64_t delay,
Callback callback) {
TimeoutQueue::Id
TimeoutQueue::add(int64_t now, int64_t delay, Callback callback) {
Id id = nextId_++;
timeouts_.insert({id, now + delay, -1, std::move(callback)});
return id;
}
TimeoutQueue::Id TimeoutQueue::addRepeating(
int64_t now,
int64_t interval,
Callback callback) {
TimeoutQueue::Id
TimeoutQueue::addRepeating(int64_t now, int64_t interval, Callback callback) {
Id id = nextId_++;
timeouts_.insert({id, now + interval, interval, std::move(callback)});
return id;
}
int64_t TimeoutQueue::nextExpiration() const {
return (timeouts_.empty() ? std::numeric_limits<int64_t>::max() :
timeouts_.get<BY_EXPIRATION>().begin()->expiration);
return (
timeouts_.empty() ? std::numeric_limits<int64_t>::max()
: timeouts_.get<BY_EXPIRATION>().begin()->expiration);
}
bool TimeoutQueue::erase(Id id) {
......@@ -51,21 +48,23 @@ int64_t TimeoutQueue::runInternal(int64_t now, bool onceOnly) {
auto& byExpiration = timeouts_.get<BY_EXPIRATION>();
int64_t nextExp;
do {
auto end = byExpiration.upper_bound(now);
const auto end = byExpiration.upper_bound(now);
std::vector<Event> expired;
std::move(byExpiration.begin(), end, std::back_inserter(expired));
byExpiration.erase(byExpiration.begin(), end);
for (auto& event : expired) {
for (const auto& event : expired) {
// Reinsert if repeating, do this before executing callbacks
// so the callbacks have a chance to call erase
if (event.repeatInterval >= 0) {
timeouts_.insert({event.id, now + event.repeatInterval,
event.repeatInterval, event.callback});
timeouts_.insert({event.id,
now + event.repeatInterval,
event.repeatInterval,
event.callback});
}
}
// Call callbacks
for (auto& event : expired) {
for (const auto& event : expired) {
event.callback(event.id, now);
}
nextExp = nextExpiration();
......
......@@ -126,7 +126,7 @@ class BasicDynamicTokenBucket {
assert(rate > 0);
assert(burstSize > 0);
return this->consumeImpl(
return consumeImpl(
rate, burstSize, nowInSeconds, [toConsume](double& tokens) {
if (tokens < toConsume) {
return false;
......@@ -160,7 +160,7 @@ class BasicDynamicTokenBucket {
assert(burstSize > 0);
double consumed;
this->consumeImpl(
consumeImpl(
rate, burstSize, nowInSeconds, [&consumed, toConsume](double& tokens) {
if (tokens < toConsume) {
consumed = tokens;
......@@ -281,7 +281,7 @@ class BasicTokenBucket {
double nowInSeconds = defaultClockNow()) noexcept {
assert(genRate > 0);
assert(burstSize > 0);
double availTokens = available(nowInSeconds);
const double availTokens = available(nowInSeconds);
rate_ = genRate;
burstSize_ = burstSize;
setCapacity(availTokens, nowInSeconds);
......
......@@ -64,7 +64,10 @@ char32_t utf8ToCodePoint(
* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
auto skip = [&] { ++p; return U'\ufffd'; };
const auto skip = [&] {
++p;
return U'\ufffd';
};
if (p >= e) {
if (skipOnError) {
......@@ -99,7 +102,7 @@ char32_t utf8ToCodePoint(
fst <<= 1;
for (unsigned int i = 1; i != 3 && p + i < e; ++i) {
unsigned char tmp = p[i];
const unsigned char tmp = p[i];
if ((tmp & 0xC0) != 0x80) {
if (skipOnError) {
......
......@@ -18,6 +18,7 @@
#include <cctype>
#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>
namespace folly {
......@@ -25,17 +26,10 @@ namespace folly {
namespace {
std::string submatch(const boost::cmatch& m, int idx) {
auto& sub = m[idx];
const auto& sub = m[idx];
return std::string(sub.first, sub.second);
}
template <class String>
void toLower(String& s) {
for (auto& c : s) {
c = char(tolower(c));
}
}
} // namespace
Uri::Uri(StringPiece str) : hasAuthority_(false), port_(0) {
......@@ -52,7 +46,7 @@ Uri::Uri(StringPiece str) : hasAuthority_(false), port_(0) {
}
scheme_ = submatch(match, 1);
toLower(scheme_);
boost::algorithm::to_lower(scheme_);
StringPiece authorityAndPath(match[2].first, match[2].second);
boost::cmatch authorityAndPathMatch;
......@@ -70,7 +64,7 @@ Uri::Uri(StringPiece str) : hasAuthority_(false), port_(0) {
// dotted-IPv4, or named host)
"(?::(\\d*))?"); // port
auto authority = authorityAndPathMatch[1];
const auto authority = authorityAndPathMatch[1];
boost::cmatch authorityMatch;
if (!boost::regex_match(authority.first,
authority.second,
......@@ -142,10 +136,10 @@ const std::vector<std::pair<std::string, std::string>>& Uri::getQueryParams() {
"([^=&]*)" /*parameter value*/
"(?=(&|$))" /*forward reference, next should be end of query or
start of next parameter*/);
boost::cregex_iterator paramBeginItr(
const boost::cregex_iterator paramBeginItr(
query_.data(), query_.data() + query_.size(), queryParamRegex);
boost::cregex_iterator paramEndItr;
for (auto itr = paramBeginItr; itr != paramEndItr; itr++) {
for (auto itr = paramBeginItr; itr != paramEndItr; ++itr) {
if (itr->length(2) == 0) {
// key is empty, ignore it
continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment