From ebdbd8a231501903831f8d1602be542ff7a717b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Mon, 24 Oct 2016 11:28:33 +0200 Subject: [PATCH] Avoid unnecessary reallocations of the "linebuf" string safeGetline() already clears the string buffer before writing to it, so the same buffer can be used multiple times, but the loops calling safeGetline() have the string scoped within the loop, so its constructed and destructed in each loop iteration, causing lots of unnecessary allocations. Parse times for some large .obj files (without asan): File A File B File C Before 2743ms 589ms 615ms After 2500ms 573ms 545ms --- tiny_obj_loader.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 2276adc..b39cb89 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -692,9 +692,8 @@ void LoadMtl(std::map *material_map, material_t material; InitMaterial(&material); + std::string linebuf; while (inStream->peek() != -1) { - std::string linebuf; - safeGetline(*inStream, linebuf); // Trim trailing whitespace. @@ -1090,8 +1089,8 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, shape_t shape; + std::string linebuf; while (inStream->peek() != -1) { - std::string linebuf; safeGetline(*inStream, linebuf); // Trim newline '\r\n' or '\n'