Use fmt and std::from_chars to make convert integer to string and convert string to integer more efficient (#6361)

* [Optimize] optimize the speed of converting integer to string

* Use fmt and std::from_chars to make convert integer to string and convert string to integer more efficient

Co-authored-by: caiconghui <caiconghui@xiaomi.com>
This commit is contained in:
caiconghui
2021-08-04 10:55:19 +08:00
committed by GitHub
parent 16bc5fa585
commit d1007afe80
11 changed files with 71 additions and 123 deletions

View File

@ -18,6 +18,7 @@
#include "exprs/cast_functions.h"
#include <cmath>
#include <fmt/format.h>
#include "exprs/anyval_util.h"
#include "gutil/strings/numbers.h"
@ -131,23 +132,15 @@ CAST_FUNCTION(FloatVal, DoubleVal, double_val)
CAST_FROM_STRINGS();
// Special-case tinyint because boost thinks it's a char and handles it differently.
// e.g. '0' is written as an empty string.
StringVal CastFunctions::cast_to_string_val(FunctionContext* ctx, const TinyIntVal& val) {
if (val.is_null) {
return StringVal::null();
}
int64_t tmp_val = val.val;
return AnyValUtil::from_string_temp(ctx, std::to_string(tmp_val));
}
#define CAST_TO_STRING(num_type) \
StringVal CastFunctions::cast_to_string_val(FunctionContext* ctx, const num_type& val) { \
if (val.is_null) return StringVal::null(); \
return AnyValUtil::from_string_temp(ctx, std::to_string(val.val)); \
auto f = fmt::format_int(val.val); \
return AnyValUtil::from_buffer_temp(ctx, f.data(), f.size()); \
}
CAST_TO_STRING(BooleanVal);
CAST_TO_STRING(TinyIntVal);
CAST_TO_STRING(SmallIntVal);
CAST_TO_STRING(IntVal);
CAST_TO_STRING(BigIntVal);
@ -156,10 +149,9 @@ StringVal CastFunctions::cast_to_string_val(FunctionContext* ctx, const LargeInt
if (val.is_null) {
return StringVal::null();
}
char buf[64];
int len = 64;
char* d = LargeIntValue::to_string(val.val, buf, &len);
return AnyValUtil::from_buffer_temp(ctx, d, len);
auto string_value = LargeIntValue::to_string(val.val);
return AnyValUtil::from_buffer_temp(ctx, string_value.data(), string_value.size());
}
template <typename T>