Skip to content

Commit 695f5ea

Browse files
authored
Performance Optimizations using std::move and constexpr (#66)
* 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. * 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.
1 parent ad4f8f6 commit 695f5ea

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

include/decimal.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@
7878
#define DEC_OVERRIDE override
7979
#endif
8080

81+
#ifdef DEC_NO_CPP11
82+
#define DEC_CONSTEXPR const
83+
#else
84+
#define DEC_CONSTEXPR constexpr
85+
#endif
86+
87+
#ifdef DEC_NO_CPP11
88+
#define DEC_MOVE(x) (x)
89+
#else
90+
#include <utility>
91+
#define DEC_MOVE(x) std::move(x)
92+
#endif
93+
8194
#if (DEC_ALLOW_SPACESHIP_OPER == 1) && (__cplusplus > 201703L)
8295
#define DEC_USE_SPACESHIP_OPER 1
8396
#else
@@ -188,27 +201,27 @@ enum {
188201
// Class definitions
189202
// ----------------------------------------------------------------------------
190203
template<int Prec> struct DecimalFactor {
191-
static const int64 value = 10 * DecimalFactor<Prec - 1>::value;
204+
static DEC_CONSTEXPR int64 value = 10 * DecimalFactor<Prec - 1>::value;
192205
};
193206

194207
template<> struct DecimalFactor<0> {
195-
static const int64 value = 1;
208+
static DEC_CONSTEXPR int64 value = 1;
196209
};
197210

198211
template<> struct DecimalFactor<1> {
199-
static const int64 value = 10;
212+
static DEC_CONSTEXPR int64 value = 10;
200213
};
201214

202215
template<int Prec, bool positive> struct DecimalFactorDiff_impl {
203-
static const int64 value = DecimalFactor<Prec>::value;
216+
static DEC_CONSTEXPR int64 value = DecimalFactor<Prec>::value;
204217
};
205218

206219
template<int Prec> struct DecimalFactorDiff_impl<Prec, false> {
207-
static const int64 value = INT64_MIN;
220+
static DEC_CONSTEXPR int64 value = INT64_MIN;
208221
};
209222

210223
template<int Prec> struct DecimalFactorDiff {
211-
static const int64 value = DecimalFactorDiff_impl<Prec, Prec >= 0>::value;
224+
static DEC_CONSTEXPR int64 value = DecimalFactorDiff_impl<Prec, Prec >= 0>::value;
212225
};
213226

214227
#ifndef DEC_EXTERNAL_ROUND
@@ -1935,7 +1948,7 @@ bool fromStream(StreamType &input, const basic_decimal_format &format, decimal_t
19351948

19361949
ostringstream out;
19371950
toStream(arg, format, out);
1938-
output = out.str();
1951+
output = DEC_MOVE(out.str());
19391952
return output;
19401953
}
19411954

@@ -1946,7 +1959,7 @@ bool fromStream(StreamType &input, const basic_decimal_format &format, decimal_t
19461959

19471960
ostringstream out;
19481961
toStream(arg, out);
1949-
output = out.str();
1962+
output = DEC_MOVE(out.str());
19501963
return output;
19511964
}
19521965

0 commit comments

Comments
 (0)