Skip to content

Commit 16412a1

Browse files
committed
ARROW-10328: [C++] Vendor fast_float number parsing library
This library is 2x to 3x faster for parsing strings to binary floating-point numbers. Closes #8494 from pitrou/ARROW-10328-fast-float Authored-by: Antoine Pitrou <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
1 parent bb4f2a0 commit 16412a1

File tree

10 files changed

+1996
-56
lines changed

10 files changed

+1996
-56
lines changed

LICENSE.txt

+8
Original file line numberDiff line numberDiff line change
@@ -2223,3 +2223,11 @@ exception of some code pulled in from other repositories (such as
22232223
public domain, released using the CC0 1.0 Universal dedication (*).
22242224

22252225
(*) https://creativecommons.org/publicdomain/zero/1.0/legalcode
2226+
2227+
--------------------------------------------------------------------------------
2228+
2229+
The files in cpp/src/arrow/vendored/fast_float/ contain code from
2230+
2231+
https://github.com/lemire/fast_float
2232+
2233+
which is made available under the Apache License 2.0.

cpp/src/arrow/util/value_parsing.cc

+5-56
Original file line numberDiff line numberDiff line change
@@ -20,70 +20,19 @@
2020
#include <string>
2121
#include <utility>
2222

23-
#include "arrow/util/double_conversion.h"
23+
#include "arrow/vendored/fast_float/fast_float.h"
2424

2525
namespace arrow {
2626
namespace internal {
2727

28-
namespace {
29-
30-
struct StringToFloatConverterImpl {
31-
StringToFloatConverterImpl()
32-
: main_converter_(flags_, main_junk_value_, main_junk_value_, "inf", "nan"),
33-
fallback_converter_(flags_, fallback_junk_value_, fallback_junk_value_, "inf",
34-
"nan") {}
35-
36-
// NOTE: This is only supported in double-conversion 3.1+
37-
static constexpr int flags_ =
38-
util::double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSIBILITY;
39-
40-
// Two unlikely values to signal a parsing error
41-
static constexpr double main_junk_value_ = 0.7066424364107089;
42-
static constexpr double fallback_junk_value_ = 0.40088499148279166;
43-
44-
util::double_conversion::StringToDoubleConverter main_converter_;
45-
util::double_conversion::StringToDoubleConverter fallback_converter_;
46-
};
47-
48-
static const StringToFloatConverterImpl g_string_to_float;
49-
50-
// Older clang versions need an explicit implementation definition.
51-
constexpr double StringToFloatConverterImpl::main_junk_value_;
52-
constexpr double StringToFloatConverterImpl::fallback_junk_value_;
53-
54-
} // namespace
55-
5628
bool StringToFloat(const char* s, size_t length, float* out) {
57-
int processed_length;
58-
float v;
59-
v = g_string_to_float.main_converter_.StringToFloat(s, static_cast<int>(length),
60-
&processed_length);
61-
if (ARROW_PREDICT_FALSE(v == static_cast<float>(g_string_to_float.main_junk_value_))) {
62-
v = g_string_to_float.fallback_converter_.StringToFloat(s, static_cast<int>(length),
63-
&processed_length);
64-
if (ARROW_PREDICT_FALSE(v ==
65-
static_cast<float>(g_string_to_float.fallback_junk_value_))) {
66-
return false;
67-
}
68-
}
69-
*out = v;
70-
return true;
29+
const auto res = ::arrow_vendored::fast_float::from_chars(s, s + length, *out);
30+
return res.ec == std::errc() && res.ptr == s + length;
7131
}
7232

7333
bool StringToFloat(const char* s, size_t length, double* out) {
74-
int processed_length;
75-
double v;
76-
v = g_string_to_float.main_converter_.StringToDouble(s, static_cast<int>(length),
77-
&processed_length);
78-
if (ARROW_PREDICT_FALSE(v == g_string_to_float.main_junk_value_)) {
79-
v = g_string_to_float.fallback_converter_.StringToDouble(s, static_cast<int>(length),
80-
&processed_length);
81-
if (ARROW_PREDICT_FALSE(v == g_string_to_float.fallback_junk_value_)) {
82-
return false;
83-
}
84-
}
85-
*out = v;
86-
return true;
34+
const auto res = ::arrow_vendored::fast_float::from_chars(s, s + length, *out);
35+
return res.ec == std::errc() && res.ptr == s + length;
8736
}
8837

8938
// ----------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The files in this directory are vendored from fast_float
2+
git changeset `dc46ad4c606dc35cb63c947496a18ef8ab1e0f44`.
3+
4+
See https://github.com/lemire/fast_float
5+
6+
Changes:
7+
- fixed include paths
8+
- disabled unused `print()` function
9+
- enclosed in `arrow_vendored` namespace.

0 commit comments

Comments
 (0)