nghttpx: Use faster version of power

In our use case, x and y is quite small, and there is no chance for
overflow, and y is always integer.
This commit is contained in:
Tatsuhiro Tsujikawa 2016-06-27 22:42:28 +09:00
parent 179561e4be
commit fd7d3c57d7
4 changed files with 16 additions and 4 deletions

View File

@ -80,8 +80,8 @@ void ConnectBlocker::on_failure() {
++fail_count_;
auto base_backoff = pow(
MULTIPLIER, static_cast<double>(std::min(MAX_BACKOFF_EXP, fail_count_)));
auto base_backoff =
util::int_pow(MULTIPLIER, std::min(MAX_BACKOFF_EXP, fail_count_));
auto dist = std::uniform_real_distribution<>(-JITTER * base_backoff,
JITTER * base_backoff);

View File

@ -157,8 +157,8 @@ constexpr auto JITTER = 0.2;
} // namespace
void LiveCheck::schedule() {
auto base_backoff = pow(
MULTIPLIER, static_cast<double>(std::min(fail_count_, MAX_BACKOFF_EXP)));
auto base_backoff =
util::int_pow(MULTIPLIER, std::min(fail_count_, MAX_BACKOFF_EXP));
auto dist = std::uniform_real_distribution<>(-JITTER * base_backoff,
JITTER * base_backoff);

View File

@ -1316,6 +1316,15 @@ StringRef percent_decode(BlockAllocator &balloc, const StringRef &src) {
return StringRef{iov.base, p};
}
// Returns x**y
double int_pow(double x, size_t y) {
auto res = 1.;
for (; y; --y) {
res *= x;
}
return res;
}
} // namespace util
} // namespace nghttp2

View File

@ -677,6 +677,9 @@ OutputIterator copy_lit(OutputIterator it, CharT(&s)[N]) {
return std::copy_n(s, N - 1, it);
}
// Returns x**y
double int_pow(double x, size_t y);
} // namespace util
} // namespace nghttp2