specialise number_converter when strtod_l is available

in load benchmark, not using the specialisation adds ~10% to execution time
This commit is contained in:
JCrawfy 2019-11-16 14:18:33 +13:00
parent ea532c5c46
commit e059d259c7

View File

@ -107,6 +107,69 @@ bool is_true(const std::string &bool_string)
#endif #endif
} }
// can find documentation for _strtod_l back to VS 2015
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtod-strtod-l-wcstod-wcstod-l?view=vs-2015
//
#if defined(_MSC_VER) && _MSC_VER >= 1900
#include <locale.h>
#include <stdlib.h>
// Cache locale object
static int c_locale_initialized = 0;
static _locale_t c_locale;
_locale_t get_c_locale()
{
if (!c_locale_initialized)
{
c_locale_initialized = 1;
c_locale = _create_locale(LC_ALL, "C");
}
return c_locale;
}
double strtod_c(const char *nptr, char **endptr)
{
return _strtod_l(nptr, endptr, get_c_locale());
}
struct number_converter
{
double stold(const std::string &s)
{
return strtod_c(s.c_str(), nullptr);
}
};
#else
// according to various sources, something like the below *would* work for POSIX systems
// however due to uncertainty on whether it will actually work it is not used
//#include <stdlib.h>
//#include <xlocale.h>
//
//// Cache locale object
//static int c_locale_initialized = 0;
//static locale_t c_locale;
//
//locale_t get_c_locale()
//{
// if (!c_locale_initialized)
// {
// c_locale_initialized = 1;
// c_locale = newlocale(LC_ALL_MASK, "C", NULL);
// }
// return c_locale;
//}
//
//double strtod_c(const char *nptr, char **endptr)
//{
// return strtod_l(nptr, endptr, get_c_locale());
//}
// add specialisations whenever possible
// in the spreadsheet-load benchmark, strtod is roughly 10% faster
struct number_converter struct number_converter
{ {
number_converter() number_converter()