Implement fish_wcstod and adopt it in builtin_test

wcstod_l is enormously slow on the Mac. This makes arithmetic comparisons
using builtin_test about 250% as fast on macOS.
This commit is contained in:
ridiculousfish
2018-11-04 15:53:31 -08:00
parent 8ebf2b8f70
commit a700acadfa
4 changed files with 42 additions and 2 deletions

View File

@@ -700,6 +700,27 @@ unsigned long long fish_wcstoull(const wchar_t *str, const wchar_t **endptr, int
return result;
}
/// Like wcstod(), but wcstod() is enormously expensive on some platforms so this tries to have a
/// fast path.
double fish_wcstod(const wchar_t *str, wchar_t **endptr) {
// The "fast path." If we're all ASCII and we fit inline, use strtod().
char narrow[128];
size_t len_plus_0 = 1 + wcslen(str);
auto is_ascii = [](wchar_t c) { return c >= 0 && c <= 127; };
if (len_plus_0 <= sizeof narrow && std::all_of(str, str + len_plus_0, is_ascii)) {
// Fast path. Copy the string into a local buffer and run strtod() on it.
std::copy(str, str + len_plus_0, narrow);
char *narrow_endptr = nullptr;
double ret = strtod(narrow, endptr ? &narrow_endptr : nullptr);
if (endptr) {
assert(narrow_endptr && "narrow_endptr should not be null");
*endptr = const_cast<wchar_t *>(str + (narrow_endptr - narrow));
}
return ret;
}
return wcstod_l(str, endptr, fish_c_locale());
}
file_id_t file_id_t::from_stat(const struct stat &buf) {
file_id_t result = {};
result.device = buf.st_dev;