-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-3320: [C++] Improve float parsing performance #2625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| // This is a private header for string-to-number parsing utilitiers | ||
|
|
||
| #ifndef ARROW_UTIL_PARSING_H | ||
| #define ARROW_UTIL_PARSING_H | ||
|
|
||
|
|
@@ -24,6 +26,8 @@ | |
| #include <string> | ||
| #include <type_traits> | ||
|
|
||
| #include <double-conversion/double-conversion.h> | ||
|
|
||
| #include "arrow/type.h" | ||
| #include "arrow/type_traits.h" | ||
|
|
||
|
|
@@ -79,26 +83,57 @@ class StringConverter<BooleanType> { | |
|
|
||
| // Ideas for faster float parsing: | ||
| // - http://rapidjson.org/md_doc_internals.html#ParsingDouble | ||
| // - https://github.com/google/double-conversion | ||
| // - https://github.com/google/double-conversion [used here] | ||
| // - https://github.com/achan001/dtoa-fast | ||
|
|
||
| template <class ARROW_TYPE> | ||
| class StringToFloatConverterMixin { | ||
| public: | ||
| using value_type = typename ARROW_TYPE::c_type; | ||
|
|
||
| StringToFloatConverterMixin() { ibuf.imbue(std::locale::classic()); } | ||
| StringToFloatConverterMixin() | ||
| : main_converter_(flags_, main_junk_value_, main_junk_value_, "inf", "nan"), | ||
| fallback_converter_(flags_, fallback_junk_value_, fallback_junk_value_, "inf", | ||
| "nan") {} | ||
|
|
||
| bool operator()(const char* s, size_t length, value_type* out) { | ||
| ibuf.clear(); | ||
| ibuf.str(std::string(s, length)); | ||
| ibuf >> *out; | ||
| // XXX Should we reset errno on failure? | ||
| return !ibuf.fail() && ibuf.eof(); | ||
| value_type v; | ||
| // double-conversion doesn't give us an error flag but signals parse | ||
| // errors with sentinel values. Since a sentinel value can appear as | ||
| // legitimate input, we fallback on a second converter with a different | ||
| // sentinel to eliminate false errors. | ||
| TryConvert(main_converter_, s, length, &v); | ||
| if (ARROW_PREDICT_FALSE(v == static_cast<value_type>(main_junk_value_))) { | ||
| TryConvert(fallback_converter_, s, length, &v); | ||
| if (ARROW_PREDICT_FALSE(v == static_cast<value_type>(fallback_junk_value_))) { | ||
| return false; | ||
| } | ||
| } | ||
| *out = v; | ||
| return true; | ||
| } | ||
|
|
||
| protected: | ||
| std::istringstream ibuf; | ||
| static const int flags_ = | ||
| double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSIBILITY; | ||
| // Two unlikely values to signal a parsing error | ||
| static constexpr double main_junk_value_ = 0.7066424364107089; | ||
| static constexpr double fallback_junk_value_ = 0.40088499148279166; | ||
|
|
||
| double_conversion::StringToDoubleConverter main_converter_; | ||
| double_conversion::StringToDoubleConverter fallback_converter_; | ||
|
|
||
| inline void TryConvert(double_conversion::StringToDoubleConverter& converter, | ||
| const char* s, size_t length, float* out) { | ||
| int processed_length; | ||
| *out = converter.StringToFloat(s, static_cast<int>(length), &processed_length); | ||
|
||
| } | ||
|
|
||
| inline void TryConvert(double_conversion::StringToDoubleConverter& converter, | ||
| const char* s, size_t length, double* out) { | ||
| int processed_length; | ||
| *out = converter.StringToDouble(s, static_cast<int>(length), &processed_length); | ||
| } | ||
| }; | ||
|
|
||
| template <> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arrow/util/parsing.his being installed, since you are including a thirdparty header, we should stop installing the headerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, the header should not be installed anymore.