From e059d259c7f1e37019b2b49ed568b8851447ee9e Mon Sep 17 00:00:00 2001 From: JCrawfy Date: Sat, 16 Nov 2019 14:18:33 +1300 Subject: [PATCH] specialise number_converter when strtod_l is available in load benchmark, not using the specialisation adds ~10% to execution time --- source/detail/serialization/xlsx_consumer.cpp | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp index 54850a6f..381c7b5e 100644 --- a/source/detail/serialization/xlsx_consumer.cpp +++ b/source/detail/serialization/xlsx_consumer.cpp @@ -107,6 +107,69 @@ bool is_true(const std::string &bool_string) #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 +#include + +// 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 +//#include +// +//// 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 { number_converter()