From 1c6ecd9c1a7a4306895621e4e5712759a5a44c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hejl?= Date: Tue, 14 Sep 2021 11:09:23 +0200 Subject: [PATCH] Replaced atof and atoi inside 3MF attribute parsing with fast_float::from_chars and boost::spirit that are faster. --- src/libslic3r/Format/3mf.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/libslic3r/Format/3mf.cpp b/src/libslic3r/Format/3mf.cpp index 295c1413d..043f951ef 100644 --- a/src/libslic3r/Format/3mf.cpp +++ b/src/libslic3r/Format/3mf.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -32,6 +33,8 @@ namespace pt = boost::property_tree; #include #include "miniz_extension.hpp" +#include + // Slightly faster than sprintf("%.9g"), but there is an issue with the karma floating point formatter, // https://github.com/boostorg/spirit/pull/586 // 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) { - const char* text = get_attribute_value_charptr(attributes, attributes_size, attribute_key); - return (text != nullptr) ? (float)::atof(text) : 0.0f; + float value = 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) { - const char* text = get_attribute_value_charptr(attributes, attributes_size, attribute_key); - return (text != nullptr) ? ::atoi(text) : 0; + int value = 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)