Improved the speed of LoadFile() in debug mode.
authorWouter van Oortmerssen <wvo@google.com>
Wed, 9 Sep 2015 19:56:23 +0000 (12:56 -0700)
committerWouter van Oortmerssen <wvo@google.com>
Wed, 9 Sep 2015 19:56:23 +0000 (12:56 -0700)
Apparently, istreambuf_iterator has a lot of overhead.

Change-Id: I804f4e8f2b380b05e939edefe0e1e88cd10e920c
Tested: on Linux.

include/flatbuffers/util.h

index 7d247dd..3951bbd 100644 (file)
@@ -111,8 +111,10 @@ inline bool FileExists(const char *name) {
 inline bool LoadFile(const char *name, bool binary, std::string *buf) {
   std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in);
   if (!ifs.is_open()) return false;
-  *buf = std::string(std::istreambuf_iterator<char>(ifs),
-                    std::istreambuf_iterator<char>());
+  ifs.seekg(0, std::ios::end);
+  (*buf).resize(ifs.tellg());
+  ifs.seekg(0, std::ios::beg);
+  ifs.read(&(*buf)[0], (*buf).size());
   return !ifs.bad();
 }