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