diff options
author | Björn Steinbrink <steinbrink@saltation.de> | 2016-10-24 11:53:00 +0200 |
---|---|---|
committer | Björn Steinbrink <steinbrink@saltation.de> | 2016-10-24 12:15:42 +0200 |
commit | aa670fe91eee32072b360d802a6d6db86bc4eb0c (patch) | |
tree | b968835f678451c199f8f16db0d7ccaab5897314 | |
parent | ebdbd8a231501903831f8d1602be542ff7a717b8 (diff) | |
download | tinyobjloader-aa670fe91eee32072b360d802a6d6db86bc4eb0c.tar.gz |
Use a lookup table to speed up float parsing
The pow() function is pretty expensive, so creating a small lookup
table for the first few negative powers of ten provides a big speedup.
Parse times for some large .obj files (without asan):
File A File B File C
Before 2500ms 573ms 545ms
After 1239ms 294ms 271ms
-rw-r--r-- | tiny_obj_loader.h | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index b39cb89..f043628 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -418,8 +418,21 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result) { read = 1; end_not_reached = (curr != s_end); 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. - 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++; curr++; end_not_reached = (curr != s_end); |