From eff9c46e59d1c893801d682e3358b74d8d74f738 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Thu, 10 Dec 2020 20:08:51 -0500 Subject: [PATCH] Replace wxTextFile for writing fp info cache wxTextFile uses wxFile which uses write syscalls which means its unbuffered. This makes file IO more expensive against network shares where it will flush far too aggressively. wxTextFile is fine for reads however. --- common/footprint_info.cpp | 3 +- include/footprint_info.h | 4 +-- pcbnew/footprint_info_impl.cpp | 66 +++++++++++++++++----------------- pcbnew/footprint_info_impl.h | 4 +-- pcbnew/pcb_base_edit_frame.cpp | 6 ++-- 5 files changed, 39 insertions(+), 44 deletions(-) diff --git a/common/footprint_info.cpp b/common/footprint_info.cpp index 23f2191631..d8a97c9291 100644 --- a/common/footprint_info.cpp +++ b/common/footprint_info.cpp @@ -143,8 +143,7 @@ FOOTPRINT_LIST* FOOTPRINT_LIST::GetInstance( KIWAY& aKiway ) if( !footprintInfo->GetCount() ) { - wxTextFile footprintInfoCache( aKiway.Prj().GetProjectPath() + "fp-info-cache" ); - footprintInfo->ReadCacheFromFile( &footprintInfoCache ); + footprintInfo->ReadCacheFromFile( aKiway.Prj().GetProjectPath() + "fp-info-cache" ); } return footprintInfo; diff --git a/include/footprint_info.h b/include/footprint_info.h index d3398d2e6e..bcd114d5b6 100644 --- a/include/footprint_info.h +++ b/include/footprint_info.h @@ -210,8 +210,8 @@ public: { } - virtual void WriteCacheToFile( wxTextFile* aFile ) { }; - virtual void ReadCacheFromFile( wxTextFile* aFile ) { }; + virtual void WriteCacheToFile( const wxString& aFilePath ) {}; + virtual void ReadCacheFromFile( const wxString& aFilePath ){}; /** * @return the number of items stored in list diff --git a/pcbnew/footprint_info_impl.cpp b/pcbnew/footprint_info_impl.cpp index bf0dc963a0..c850bb3f48 100644 --- a/pcbnew/footprint_info_impl.cpp +++ b/pcbnew/footprint_info_impl.cpp @@ -37,6 +37,8 @@ #include #include #include +#include +#include #include #include @@ -339,59 +341,55 @@ FOOTPRINT_LIST_IMPL::~FOOTPRINT_LIST_IMPL() } -void FOOTPRINT_LIST_IMPL::WriteCacheToFile( wxTextFile* aCacheFile ) +void FOOTPRINT_LIST_IMPL::WriteCacheToFile( const wxString& aFilePath ) { - if( aCacheFile->Exists() ) - { - if( !aCacheFile->Open() ) - return; + wxFFileOutputStream outStream( aFilePath ); + wxTextOutputStream txtStream( outStream ); - aCacheFile->Clear(); - } - else + if( !outStream.IsOk() ) { - if( !aCacheFile->Create() ) - return; + return; } - aCacheFile->AddLine( wxString::Format( "%lld", m_list_timestamp ) ); + txtStream << wxString::Format( "%lld", m_list_timestamp ) << endl; - for( auto& fpinfo : m_list ) + for( std::unique_ptr& fpinfo : m_list ) { - aCacheFile->AddLine( fpinfo->GetLibNickname() ); - aCacheFile->AddLine( fpinfo->GetName() ); - aCacheFile->AddLine( EscapeString( fpinfo->GetDescription() ) ); - aCacheFile->AddLine( EscapeString( fpinfo->GetKeywords() ) ); - aCacheFile->AddLine( wxString::Format( "%d", fpinfo->GetOrderNum() ) ); - aCacheFile->AddLine( wxString::Format( "%u", fpinfo->GetPadCount() ) ); - aCacheFile->AddLine( wxString::Format( "%u", fpinfo->GetUniquePadCount() ) ); + txtStream << fpinfo->GetLibNickname() << endl; + txtStream << fpinfo->GetName() << endl; + txtStream << EscapeString( fpinfo->GetDescription() ) << endl; + txtStream << EscapeString( fpinfo->GetKeywords() ) << endl; + txtStream << wxString::Format( "%d", fpinfo->GetOrderNum() ) << endl; + txtStream << wxString::Format( "%u", fpinfo->GetPadCount() ) << endl; + txtStream << wxString::Format( "%u", fpinfo->GetUniquePadCount() ) << endl; } - aCacheFile->Write(); - aCacheFile->Close(); + txtStream.Flush(); } -void FOOTPRINT_LIST_IMPL::ReadCacheFromFile( wxTextFile* aCacheFile ) +void FOOTPRINT_LIST_IMPL::ReadCacheFromFile( const wxString& aFilePath ) { + wxTextFile cacheFile( aFilePath ); + m_list_timestamp = 0; m_list.clear(); try { - if( aCacheFile->Exists() && aCacheFile->Open() ) + if( cacheFile.Exists() && cacheFile.Open() ) { - aCacheFile->GetFirstLine().ToLongLong( &m_list_timestamp ); + cacheFile.GetFirstLine().ToLongLong( &m_list_timestamp ); - while( aCacheFile->GetCurrentLine() + 6 < aCacheFile->GetLineCount() ) + while( cacheFile.GetCurrentLine() + 6 < cacheFile.GetLineCount() ) { - wxString libNickname = aCacheFile->GetNextLine(); - wxString name = aCacheFile->GetNextLine(); - wxString description = UnescapeString( aCacheFile->GetNextLine() ); - wxString keywords = UnescapeString( aCacheFile->GetNextLine() ); - int orderNum = wxAtoi( aCacheFile->GetNextLine() ); - unsigned int padCount = (unsigned) wxAtoi( aCacheFile->GetNextLine() ); - unsigned int uniquePadCount = (unsigned) wxAtoi( aCacheFile->GetNextLine() ); + wxString libNickname = cacheFile.GetNextLine(); + wxString name = cacheFile.GetNextLine(); + wxString description = UnescapeString( cacheFile.GetNextLine() ); + wxString keywords = UnescapeString( cacheFile.GetNextLine() ); + int orderNum = wxAtoi( cacheFile.GetNextLine() ); + unsigned int padCount = (unsigned) wxAtoi( cacheFile.GetNextLine() ); + unsigned int uniquePadCount = (unsigned) wxAtoi( cacheFile.GetNextLine() ); auto* fpinfo = new FOOTPRINT_INFO_IMPL( libNickname, name, description, keywords, orderNum, padCount, uniquePadCount ); @@ -409,6 +407,6 @@ void FOOTPRINT_LIST_IMPL::ReadCacheFromFile( wxTextFile* aCacheFile ) if( m_list.size() == 0 ) m_list_timestamp = 0; - if( aCacheFile->IsOpened() ) - aCacheFile->Close(); + if( cacheFile.IsOpened() ) + cacheFile.Close(); } diff --git a/pcbnew/footprint_info_impl.h b/pcbnew/footprint_info_impl.h index 8ba047b242..697209fe50 100644 --- a/pcbnew/footprint_info_impl.h +++ b/pcbnew/footprint_info_impl.h @@ -117,8 +117,8 @@ public: FOOTPRINT_LIST_IMPL(); virtual ~FOOTPRINT_LIST_IMPL(); - void WriteCacheToFile( wxTextFile* aFile ) override; - void ReadCacheFromFile( wxTextFile* aFile ) override; + void WriteCacheToFile( const wxString& aFilePath ) override; + void ReadCacheFromFile( const wxString& aFilePath ) override; bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr, PROGRESS_REPORTER* aProgressReporter = nullptr ) override; diff --git a/pcbnew/pcb_base_edit_frame.cpp b/pcbnew/pcb_base_edit_frame.cpp index c205c61c9f..2e2e6b3bf5 100644 --- a/pcbnew/pcb_base_edit_frame.cpp +++ b/pcbnew/pcb_base_edit_frame.cpp @@ -41,8 +41,7 @@ PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent, { if( !GFootprintList.GetCount() ) { - wxTextFile footprintInfoCache( Prj().GetProjectPath() + "fp-info-cache" ); - GFootprintList.ReadCacheFromFile( &footprintInfoCache ); + GFootprintList.ReadCacheFromFile( Prj().GetProjectPath() + "fp-info-cache" ); } } @@ -50,8 +49,7 @@ PCB_BASE_EDIT_FRAME::~PCB_BASE_EDIT_FRAME() { if( wxFileName::IsDirWritable( Prj().GetProjectPath() ) ) { - wxTextFile footprintInfoCache( Prj().GetProjectPath() + "fp-info-cache" ); - GFootprintList.WriteCacheToFile( &footprintInfoCache ); + GFootprintList.WriteCacheToFile( Prj().GetProjectPath() + "fp-info-cache" ); } }