From ff1300d13b113722e0e9eafd8a2fef24fb4ba002 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 26 Feb 2021 11:28:21 -0800 Subject: [PATCH] FABMASTER: Preallocate vectors Reserves memory for vector elements based on file size. This helps reduce the number of re-allocations needed when reading large files --- pcbnew/plugins/fabmaster/import_fabmaster.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pcbnew/plugins/fabmaster/import_fabmaster.cpp b/pcbnew/plugins/fabmaster/import_fabmaster.cpp index 3c5dd6eec4..18d40cf8fb 100644 --- a/pcbnew/plugins/fabmaster/import_fabmaster.cpp +++ b/pcbnew/plugins/fabmaster/import_fabmaster.cpp @@ -86,9 +86,22 @@ bool FABMASTER::Read( const std::string& aFile ) m_filename = aFile; + // Read/ignore all bytes in the file to find the size and then go back to the beginning + ifs.ignore( std::numeric_limits::max() ); + std::streamsize length = ifs.gcount(); + ifs.clear(); + ifs.seekg( 0, std::ios_base::beg ); + std::string buffer( std::istreambuf_iterator{ ifs }, {} ); + std::vector < std::string > row; + + // Reserve an estimate of the number of rows to prevent continual re-allocation + // crashing (Looking at you MSVC) + row.reserve( length / 100 ); std::string cell; + cell.reserve( 100 ); + bool quoted = false; for( auto ch : buffer ) @@ -161,6 +174,9 @@ FABMASTER::section_type FABMASTER::detectType( size_t aOffset ) if( row.size() < 3 ) return UNKNOWN_EXTRACT; + if( row[0].back() != 'A' ) + return UNKNOWN_EXTRACT; + std::string row1 = row[1]; std::string row2 = row[2]; std::string row3{};