From 4f9407756bc221924f90a28a0f689d42453daef3 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Wed, 21 Aug 2024 19:20:15 -0700 Subject: [PATCH 1/2] Replaced const with constexpr, to allow compiler to define various things at compile time rather than run time. This may have a small bump on performance, but either way it can't hurt. --- include/decimal.h | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/include/decimal.h b/include/decimal.h index 3378d0a..4994527 100644 --- a/include/decimal.h +++ b/include/decimal.h @@ -78,6 +78,12 @@ #define DEC_OVERRIDE override #endif +#ifdef DEC_NO_CPP11 +#define DEC_CONSTEXPR const +#else +#define DEC_CONSTEXPR constexpr +#endif + #if (DEC_ALLOW_SPACESHIP_OPER == 1) && (__cplusplus > 201703L) #define DEC_USE_SPACESHIP_OPER 1 #else @@ -188,27 +194,27 @@ enum { // Class definitions // ---------------------------------------------------------------------------- template struct DecimalFactor { - static const int64 value = 10 * DecimalFactor::value; + static DEC_CONSTEXPR int64 value = 10 * DecimalFactor::value; }; template<> struct DecimalFactor<0> { - static const int64 value = 1; + static DEC_CONSTEXPR int64 value = 1; }; template<> struct DecimalFactor<1> { - static const int64 value = 10; + static DEC_CONSTEXPR int64 value = 10; }; template struct DecimalFactorDiff_impl { - static const int64 value = DecimalFactor::value; + static DEC_CONSTEXPR int64 value = DecimalFactor::value; }; template struct DecimalFactorDiff_impl { - static const int64 value = INT64_MIN; + static DEC_CONSTEXPR int64 value = INT64_MIN; }; template struct DecimalFactorDiff { - static const int64 value = DecimalFactorDiff_impl= 0>::value; + static DEC_CONSTEXPR int64 value = DecimalFactorDiff_impl= 0>::value; }; #ifndef DEC_EXTERNAL_ROUND From f762ce54ce07cad30c83e965abd8755746cfd682 Mon Sep 17 00:00:00 2001 From: Evan Porter Date: Wed, 21 Aug 2024 19:35:26 -0700 Subject: [PATCH 2/2] Added move semantics (via std::move within the utility lib). This has a small perfomance benefit since it will move the value rather than copy it. I only made these changes within the toString functions where I know the std::library is being used. --- include/decimal.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/include/decimal.h b/include/decimal.h index 4994527..7cb0e81 100644 --- a/include/decimal.h +++ b/include/decimal.h @@ -84,6 +84,13 @@ #define DEC_CONSTEXPR constexpr #endif +#ifdef DEC_NO_CPP11 + #define DEC_MOVE(x) (x) +#else + #include + #define DEC_MOVE(x) std::move(x) +#endif + #if (DEC_ALLOW_SPACESHIP_OPER == 1) && (__cplusplus > 201703L) #define DEC_USE_SPACESHIP_OPER 1 #else @@ -1941,7 +1948,7 @@ bool fromStream(StreamType &input, const basic_decimal_format &format, decimal_t ostringstream out; toStream(arg, format, out); - output = out.str(); + output = DEC_MOVE(out.str()); return output; } @@ -1952,7 +1959,7 @@ bool fromStream(StreamType &input, const basic_decimal_format &format, decimal_t ostringstream out; toStream(arg, out); - output = out.str(); + output = DEC_MOVE(out.str()); return output; }