Merge pull request #4775

741e4a11 epee: speed up json number parsing (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2018-11-16 11:00:56 +02:00
commit 3880cae134
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD

View File

@ -125,16 +125,22 @@ namespace epee
{ {
if(is_signed) if(is_signed)
{ {
int64_t nval = boost::lexical_cast<int64_t>(val); errno = 0;
int64_t nval = strtoll(val.c_str(), NULL, 10);
if (errno) throw std::runtime_error("Invalid number: " + val);
stg.set_value(name, nval, current_section); stg.set_value(name, nval, current_section);
}else }else
{ {
uint64_t nval = boost::lexical_cast<uint64_t >(val); errno = 0;
uint64_t nval = strtoull(val.c_str(), NULL, 10);
if (errno) throw std::runtime_error("Invalid number: " + val);
stg.set_value(name, nval, current_section); stg.set_value(name, nval, current_section);
} }
}else }else
{ {
double nval = boost::lexical_cast<double>(val); errno = 0;
double nval = strtod(val.c_str(), NULL);
if (errno) throw std::runtime_error("Invalid number: " + val);
stg.set_value(name, nval, current_section); stg.set_value(name, nval, current_section);
} }
state = match_state_wonder_after_value; state = match_state_wonder_after_value;
@ -208,12 +214,25 @@ namespace epee
match_number2(it, buf_end, val, is_v_float, is_signed_val); match_number2(it, buf_end, val, is_v_float, is_signed_val);
if(!is_v_float) if(!is_v_float)
{ {
int64_t nval = boost::lexical_cast<int64_t>(val);//bool res = string_tools::string_to_num_fast(val, nval); if (is_signed_val)
h_array = stg.insert_first_value(name, nval, current_section); {
errno = 0;
int64_t nval = strtoll(val.c_str(), NULL, 10);
if (errno) throw std::runtime_error("Invalid number: " + val);
h_array = stg.insert_first_value(name, nval, current_section);
}else
{
errno = 0;
uint64_t nval = strtoull(val.c_str(), NULL, 10);
if (errno) throw std::runtime_error("Invalid number: " + val);
h_array = stg.insert_first_value(name, nval, current_section);
}
CHECK_AND_ASSERT_THROW_MES(h_array, " failed to insert values section entry"); CHECK_AND_ASSERT_THROW_MES(h_array, " failed to insert values section entry");
}else }else
{ {
double nval = boost::lexical_cast<double>(val);//bool res = string_tools::string_to_num_fast(val, nval); errno = 0;
double nval = strtod(val.c_str(), NULL);
if (errno) throw std::runtime_error("Invalid number: " + val);
h_array = stg.insert_first_value(name, nval, current_section); h_array = stg.insert_first_value(name, nval, current_section);
CHECK_AND_ASSERT_THROW_MES(h_array, " failed to insert values section entry"); CHECK_AND_ASSERT_THROW_MES(h_array, " failed to insert values section entry");
} }
@ -286,13 +305,24 @@ namespace epee
bool insert_res = false; bool insert_res = false;
if(!is_v_float) if(!is_v_float)
{ {
int64_t nval = boost::lexical_cast<int64_t>(val); //bool res = string_tools::string_to_num_fast(val, nval); if (is_signed_val)
insert_res = stg.insert_next_value(h_array, nval); {
errno = 0;
int64_t nval = strtoll(val.c_str(), NULL, 10);
if (errno) throw std::runtime_error("Invalid number: " + val);
insert_res = stg.insert_next_value(h_array, nval);
}else
{
errno = 0;
uint64_t nval = strtoull(val.c_str(), NULL, 10);
if (errno) throw std::runtime_error("Invalid number: " + val);
insert_res = stg.insert_next_value(h_array, nval);
}
}else }else
{ {
//TODO: optimize here if need errno = 0;
double nval = boost::lexical_cast<double>(val); //string_tools::string_to_num_fast(val, nval); double nval = strtod(val.c_str(), NULL);
if (errno) throw std::runtime_error("Invalid number: " + val);
insert_res = stg.insert_next_value(h_array, nval); insert_res = stg.insert_next_value(h_array, nval);
} }
CHECK_AND_ASSERT_THROW_MES(insert_res, "Failed to insert next value"); CHECK_AND_ASSERT_THROW_MES(insert_res, "Failed to insert next value");