From 01e06b69a57019623827c5bce2c05f5f29db204b Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 9 Sep 2015 12:56:23 -0700 Subject: [PATCH] Improved the speed of LoadFile() in debug mode. Apparently, istreambuf_iterator has a lot of overhead. Change-Id: I804f4e8f2b380b05e939edefe0e1e88cd10e920c Tested: on Linux. --- include/flatbuffers/util.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index 7d247dd07..3951bbdd7 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -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(ifs), - std::istreambuf_iterator()); + 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(); }