@@ -175,21 +175,33 @@ int main(int argc, char** argv) {
175175 std::vector<std::pair<char *, size_t >> input_buffers;
176176
177177 std::stringstream list_of_input_files (FLAGS_inputs);
178- std::string token;
178+ std::string path;
179+
180+ // First reserve memory for number of vector elements to avoid vector
181+ // reallocations when emplacing back.
182+ std::vector<std::string> file_paths;
183+ while (std::getline (list_of_input_files, path, ' ,' )) {
184+ file_paths.push_back (std::move (path));
185+ }
186+ inputs_storage.reserve (file_paths.size ());
187+
188+ for (const auto & file_path : file_paths) {
189+ std::ifstream input_file_handle (
190+ file_path, std::ios::binary | std::ios::ate);
179191
180- while (std::getline (list_of_input_files, token, ' ,' )) {
181- std::ifstream input_file_handle (token, std::ios::binary | std::ios::ate);
182192 if (!input_file_handle) {
183- ET_LOG (Error, " Failed to open input file: %s\n " , token .c_str ());
193+ ET_LOG (Error, " Failed to open input file: %s\n " , file_path .c_str ());
184194 return 1 ;
185195 }
186196
187197 std::streamsize file_size = input_file_handle.tellg ();
188198 input_file_handle.seekg (0 , std::ios::beg);
189199
200+ // Reserve memory for actual file contents.
190201 inputs_storage.emplace_back (file_size, ' \0 ' );
202+
191203 if (!input_file_handle.read (&inputs_storage.back ()[0 ], file_size)) {
192- ET_LOG (Error, " Failed to read input file: %s\n " , token .c_str ());
204+ ET_LOG (Error, " Failed to read input file: %s\n " , file_path .c_str ());
193205 return 1 ;
194206 }
195207
0 commit comments