From fd7d3c57d7ecd75567c3c71d7deabc2c2e3b9b7f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 27 Jun 2016 22:42:28 +0900 Subject: [PATCH] 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. --- src/shrpx_connect_blocker.cc | 4 ++-- src/shrpx_live_check.cc | 4 ++-- src/util.cc | 9 +++++++++ src/util.h | 3 +++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/shrpx_connect_blocker.cc b/src/shrpx_connect_blocker.cc index 164dea86..4ffe29ca 100644 --- a/src/shrpx_connect_blocker.cc +++ b/src/shrpx_connect_blocker.cc @@ -80,8 +80,8 @@ void ConnectBlocker::on_failure() { ++fail_count_; - auto base_backoff = pow( - MULTIPLIER, static_cast(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); diff --git a/src/shrpx_live_check.cc b/src/shrpx_live_check.cc index ba0d150d..7f6559c8 100644 --- a/src/shrpx_live_check.cc +++ b/src/shrpx_live_check.cc @@ -157,8 +157,8 @@ constexpr auto JITTER = 0.2; } // namespace void LiveCheck::schedule() { - auto base_backoff = pow( - MULTIPLIER, static_cast(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); diff --git a/src/util.cc b/src/util.cc index 5a450bec..d24c5c8d 100644 --- a/src/util.cc +++ b/src/util.cc @@ -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 diff --git a/src/util.h b/src/util.h index 6d92529b..375dacfd 100644 --- a/src/util.h +++ b/src/util.h @@ -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