From b8f7fa0318d726faf6badd24d2974b705e83c1b9 Mon Sep 17 00:00:00 2001 From: Edward Hennis Date: Thu, 23 May 2019 12:04:05 -0400 Subject: [PATCH] [FOLD] Review feedback from @mellery451: * Convert scaleFeeLoad worker functions to lambdas. * Simplify to_string(quantity). * Rename detail::clamp to detail::clamp32. --- src/ripple/app/misc/impl/LoadFeeTrack.cpp | 130 ++++++---------------- src/ripple/basics/feeunits.h | 10 +- 2 files changed, 40 insertions(+), 100 deletions(-) diff --git a/src/ripple/app/misc/impl/LoadFeeTrack.cpp b/src/ripple/app/misc/impl/LoadFeeTrack.cpp index 153f26c953a..5a31e7f153c 100644 --- a/src/ripple/app/misc/impl/LoadFeeTrack.cpp +++ b/src/ripple/app/misc/impl/LoadFeeTrack.cpp @@ -82,22 +82,7 @@ LoadFeeTrack::lowerLocalFee () //------------------------------------------------------------------------------ -// NIKB TODO: Once we get C++17, we can replace lowestTerms -// with this: -// -// template && -// std::is_integral_v> -// > -// void lowestTerms(T1& a, T2& b) -// { -// if (auto const gcd = std::gcd(a, b)) -// { -// a /= gcd; -// b /= gcd; -// } -// } +// EHENNIS TODO: Once we get C++17, we can replace gcd with std::gcd template ::value && - sizeof(T1) <= sizeof(std::uint64_t) >, - class = std::enable_if_t < - std::is_unsigned::value && - sizeof(T2) <= sizeof(std::uint64_t) > -> -void lowestTerms(T1& a, T2& b) -{ - if (auto const g = gcd(a, b)) - { - a /= g; - b /= g; - } -} - -// Normally, these types wouldn't be mathematically compatible. -// For this file only, it's ok. -template ::value && - sizeof(T1) <= sizeof(std::uint64_t) >, - class = std::enable_if_t < - std::is_unsigned::value && - sizeof(T2) <= sizeof(std::uint64_t)>, - class = std::enable_if_t < - std::is_same_v || - std::is_same_v - > -> -void lowestTerms(boost::units::quantity& a, - FeeUnit& b) -{ - if (auto const g = gcd(a.value(), b.value())) - { - a /= g; - b /= g; - } -} - -template ::value && - sizeof(T1) <= sizeof(std::uint64_t) >, - class = std::enable_if_t < - std::is_unsigned::value && - sizeof(T2) <= sizeof(std::uint64_t) > -> -void lowestTerms(T1& a, FeeUnit& b) -{ - if (auto const g = gcd(a, b.value())) - { - a /= g; - b /= g; - } -} - -// Normally, these types wouldn't be swappable. For this file only, it's ok. -template -static -void -maybe_swap (FeeUnit& lhs, Drops& rhs) -{ - if (lhs.value() < rhs.value()) - { - Drops tmp = lhs.value() * units::drops; - lhs = rhs.value() * units::feeunits; - rhs = tmp; - } - // double check - assert(lhs.value() >= rhs.value()); -} - // Scale using load as well as base rate Drops64 scaleFeeLoad(FeeUnit64 fee, LoadFeeTrack const& feeTrack, Fees const& fees, bool bUnlimited) { + auto lowestTerms1 = [](auto& a, auto& b) + { + if (auto const g = gcd(a, b.value())) + { + a /= g; + b /= g; + } + }; + + // Normally, types with different units wouldn't be mathematically + // compatible. For this file only, it's ok. + auto lowestTerms2 = [](auto& a, auto& b) + { + if (auto const g = gcd(a.value(), b.value())) + { + a /= g; + b /= g; + } + }; + + // Normally, these types wouldn't be swappable. For this file only, it's ok. + auto maybe_swap = [](auto& lhs, auto& rhs) + { + if (lhs.value() < rhs.value()) + { + auto tmp = lhs.value() * units::drops; + lhs = rhs.value() * units::feeunits; + rhs = tmp; + } + // double check + assert(lhs.value() >= rhs.value()); + }; + if (fee == 0 * units::feeunit) return 0; std::uint32_t feeFactor; @@ -226,9 +170,9 @@ scaleFeeLoad(FeeUnit64 fee, LoadFeeTrack const& feeTrack, * safe_cast(feeTrack.getLoadBase()); // Reduce fee * baseFee * feeFactor / (fees.units * lftNormalFee) // to lowest terms. - lowestTerms(fee, den); - lowestTerms(baseFee, den); - lowestTerms(feeFactor, den); + lowestTerms2(fee, den); + lowestTerms2(baseFee, den); + lowestTerms1(feeFactor, den); // fee and baseFee are 64 bit, feeFactor is 32 bit // Order fee and baseFee largest first diff --git a/src/ripple/basics/feeunits.h b/src/ripple/basics/feeunits.h index 9795ef8bde8..b363e58f642 100644 --- a/src/ripple/basics/feeunits.h +++ b/src/ripple/basics/feeunits.h @@ -68,11 +68,7 @@ operator<<(std::basic_ostream& os, template std::string to_string(const quantity& t) { - std::stringstream sstr; - - sstr << t; - - return sstr.str(); + return std::to_string(t.value()); } } // units @@ -319,7 +315,7 @@ namespace detail // Need to cap to uint64 to uint32 due to JSON limitations template boost::units::quantity -clamp(boost::units::quantity v) +clamp32(boost::units::quantity v) { constexpr Unit unit; constexpr boost::units::quantity max32 = @@ -349,7 +345,7 @@ Json::Value maybe_toJson(boost::units::quantity src, std::false_type) { - return maybe_toJson(clamp(src), std::true_type{}); + return maybe_toJson(clamp32(src), std::true_type{}); } }