Merge pull request #105 from dotdash/speedup

Improve parsing speed by about a factor of 2 for large files
This commit is contained in:
Syoyo Fujita
2016-10-24 23:37:34 +09:00
committed by GitHub

View File

@@ -418,8 +418,21 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) {
read = 1; read = 1;
end_not_reached = (curr != s_end); end_not_reached = (curr != s_end);
while (end_not_reached && IS_DIGIT(*curr)) { while (end_not_reached && IS_DIGIT(*curr)) {
static const double pow_lut[] = {
1.0,
0.1,
0.01,
0.001,
0.0001,
0.00001,
0.000001,
0.0000001,
};
const int lut_entries = sizeof pow_lut / sizeof pow_lut[0];
// NOTE: Don't use powf here, it will absolutely murder precision. // NOTE: Don't use powf here, it will absolutely murder precision.
mantissa += static_cast<int>(*curr - 0x30) * pow(10.0, -read); mantissa += static_cast<int>(*curr - 0x30) *
(read < lut_entries ? pow_lut[read] : pow(10.0, -read));
read++; read++;
curr++; curr++;
end_not_reached = (curr != s_end); end_not_reached = (curr != s_end);
@@ -459,8 +472,8 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) {
} }
assemble: assemble:
*result = *result = (sign == '+' ? 1 : -1) *
(sign == '+' ? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent); (exponent ? ldexp(mantissa * pow(5.0, exponent), exponent) : mantissa);
return true; return true;
fail: fail:
return false; return false;
@@ -692,9 +705,8 @@ void LoadMtl(std::map<std::string, int> *material_map,
material_t material; material_t material;
InitMaterial(&material); InitMaterial(&material);
std::string linebuf;
while (inStream->peek() != -1) { while (inStream->peek() != -1) {
std::string linebuf;
safeGetline(*inStream, linebuf); safeGetline(*inStream, linebuf);
// Trim trailing whitespace. // Trim trailing whitespace.
@@ -1090,8 +1102,8 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
shape_t shape; shape_t shape;
std::string linebuf;
while (inStream->peek() != -1) { while (inStream->peek() != -1) {
std::string linebuf;
safeGetline(*inStream, linebuf); safeGetline(*inStream, linebuf);
// Trim newline '\r\n' or '\n' // Trim newline '\r\n' or '\n'