@@ -175,21 +175,33 @@ int main(int argc, char** argv) {
175
175
std::vector<std::pair<char *, size_t >> input_buffers;
176
176
177
177
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);
179
191
180
- while (std::getline (list_of_input_files, token, ' ,' )) {
181
- std::ifstream input_file_handle (token, std::ios::binary | std::ios::ate);
182
192
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 ());
184
194
return 1 ;
185
195
}
186
196
187
197
std::streamsize file_size = input_file_handle.tellg ();
188
198
input_file_handle.seekg (0 , std::ios::beg);
189
199
200
+ // Reserve memory for actual file contents.
190
201
inputs_storage.emplace_back (file_size, ' \0 ' );
202
+
191
203
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 ());
193
205
return 1 ;
194
206
}
195
207
0 commit comments