Skip to content

Commit

Permalink
nghttpx: Use faster version of power
Browse files Browse the repository at this point in the history
In our use case, x and y is quite small, and there is no chance for
overflow, and y is always integer.
  • Loading branch information
tatsuhiro-t committed Jun 27, 2016
1 parent 179561e commit fd7d3c5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/shrpx_connect_blocker.cc
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/shrpx_live_check.cc
Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions src/util.cc
Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/util.h
Expand Up @@ -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
Expand Down

0 comments on commit fd7d3c5

Please sign in to comment.