Replaced atof and atoi inside 3MF attribute parsing with fast_float::from_chars and boost::spirit that are faster.

This commit is contained in:
Lukáš Hejl 2021-09-14 11:09:23 +02:00
parent 680a0665de
commit 1c6ecd9c1a

View File

@ -21,6 +21,7 @@
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/spirit/include/karma.hpp> #include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/qi_int.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
@ -32,6 +33,8 @@ namespace pt = boost::property_tree;
#include <Eigen/Dense> #include <Eigen/Dense>
#include "miniz_extension.hpp" #include "miniz_extension.hpp"
#include <fast_float/fast_float.h>
// Slightly faster than sprintf("%.9g"), but there is an issue with the karma floating point formatter, // Slightly faster than sprintf("%.9g"), but there is an issue with the karma floating point formatter,
// https://github.com/boostorg/spirit/pull/586 // https://github.com/boostorg/spirit/pull/586
// where the exported string is one digit shorter than it should be to guarantee lossless round trip. // where the exported string is one digit shorter than it should be to guarantee lossless round trip.
@ -172,14 +175,18 @@ std::string get_attribute_value_string(const char** attributes, unsigned int att
float get_attribute_value_float(const char** attributes, unsigned int attributes_size, const char* attribute_key) float get_attribute_value_float(const char** attributes, unsigned int attributes_size, const char* attribute_key)
{ {
const char* text = get_attribute_value_charptr(attributes, attributes_size, attribute_key); float value = 0.0f;
return (text != nullptr) ? (float)::atof(text) : 0.0f; if (const char *text = get_attribute_value_charptr(attributes, attributes_size, attribute_key); text != nullptr)
fast_float::from_chars(text, text + strlen(text), value);
return value;
} }
int get_attribute_value_int(const char** attributes, unsigned int attributes_size, const char* attribute_key) int get_attribute_value_int(const char** attributes, unsigned int attributes_size, const char* attribute_key)
{ {
const char* text = get_attribute_value_charptr(attributes, attributes_size, attribute_key); int value = 0;
return (text != nullptr) ? ::atoi(text) : 0; if (const char *text = get_attribute_value_charptr(attributes, attributes_size, attribute_key); text != nullptr)
boost::spirit::qi::parse(text, text + strlen(text), boost::spirit::qi::int_, value);
return value;
} }
bool get_attribute_value_bool(const char** attributes, unsigned int attributes_size, const char* attribute_key) bool get_attribute_value_bool(const char** attributes, unsigned int attributes_size, const char* attribute_key)