From 2fb2eac4d576cd13e8da80bbc30168b9482c9c82 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Fri, 24 Apr 2020 21:33:59 +0100 Subject: [PATCH] Performance enhancements. --- common/common.cpp | 4 +++- common/eda_text.cpp | 8 ++++++++ eeschema/sch_field.cpp | 32 ++++++++++++++++---------------- eeschema/sch_painter.cpp | 12 ++++++------ eeschema/sch_text.cpp | 16 ++++++++++------ include/eda_text.h | 18 +++++++++++++++--- pcbnew/class_pcb_text.cpp | 5 +++-- pcbnew/class_text_mod.cpp | 19 ++++++++++++------- 8 files changed, 73 insertions(+), 41 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 04aa4d0191..875bc1a6c5 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -379,6 +379,8 @@ wxString ExpandTextVars( const wxString& aSource, wxString newbuf; size_t sourceLen = aSource.length(); + newbuf.Alloc( sourceLen ); // best guess (improves performance) + for( size_t i = 0; i < sourceLen; ++i ) { if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' ) @@ -428,7 +430,7 @@ wxString KIwxExpandEnvVars( const wxString& str, const PROJECT* aProject ) size_t strlen = str.length(); wxString strResult; - strResult.Alloc( strlen ); + strResult.Alloc( strlen ); // best guess (improves performance) for( size_t n = 0; n < strlen; n++ ) { diff --git a/common/eda_text.cpp b/common/eda_text.cpp index 033ce019db..1de0589d44 100644 --- a/common/eda_text.cpp +++ b/common/eda_text.cpp @@ -91,7 +91,10 @@ EDA_TEXT::EDA_TEXT( const wxString& text ) : SetTextSize( wxSize( sz, sz ) ); if( !text.IsEmpty() ) + { m_shown_text = UnescapeString( text ); + m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ); + } } @@ -100,6 +103,7 @@ EDA_TEXT::EDA_TEXT( const EDA_TEXT& aText ) : m_e( aText.m_e ) { m_shown_text = UnescapeString( m_text ); + m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ); } @@ -112,6 +116,7 @@ void EDA_TEXT::SetText( const wxString& aText ) { m_text = aText; m_shown_text = UnescapeString( aText ); + m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ); } @@ -119,6 +124,7 @@ void EDA_TEXT::CopyText( const EDA_TEXT& aSrc ) { m_text = aSrc.m_text; m_shown_text = aSrc.m_shown_text; + m_shown_text_has_text_var_refs = aSrc.m_shown_text_has_text_var_refs; } @@ -132,6 +138,7 @@ void EDA_TEXT::SwapText( EDA_TEXT& aTradingPartner ) { std::swap( m_text, aTradingPartner.m_text ); std::swap( m_shown_text, aTradingPartner.m_shown_text ); + std::swap( m_shown_text_has_text_var_refs, aTradingPartner.m_shown_text_has_text_var_refs ); } @@ -164,6 +171,7 @@ bool EDA_TEXT::Replace( wxFindReplaceData& aSearchData ) { bool retval = EDA_ITEM::Replace( aSearchData, m_text ); m_shown_text = UnescapeString( m_text ); + m_shown_text_has_text_var_refs = m_shown_text.Contains( wxT( "${" ) ); return retval; } diff --git a/eeschema/sch_field.cpp b/eeschema/sch_field.cpp index 5fd3ad37bd..8ce385cfa9 100644 --- a/eeschema/sch_field.cpp +++ b/eeschema/sch_field.cpp @@ -90,19 +90,23 @@ wxString SCH_FIELD::GetShownText( int aDepth ) const }; PROJECT* project = nullptr; - wxString text = EDA_TEXT::GetShownText(); + bool processTextVars = false; + wxString text = EDA_TEXT::GetShownText( &processTextVars ); - if( g_RootSheet && g_RootSheet->GetScreen() ) - project = &g_RootSheet->GetScreen()->Kiway().Prj(); - - if( aDepth < 10 ) + if( processTextVars ) { - if( m_Parent && m_Parent->Type() == SCH_COMPONENT_T ) - text = ExpandTextVars( text, &symbolResolver, project ); - else if( m_Parent && m_Parent->Type() == SCH_SHEET_T ) - text = ExpandTextVars( text, &sheetResolver, project ); - else - text = ExpandTextVars( text, nullptr, project ); + if( g_RootSheet && g_RootSheet->GetScreen() ) + project = &g_RootSheet->GetScreen()->Kiway().Prj(); + + if( aDepth < 10 ) + { + if( m_Parent && m_Parent->Type() == SCH_COMPONENT_T ) + text = ExpandTextVars( text, &symbolResolver, project ); + else if( m_Parent && m_Parent->Type() == SCH_SHEET_T ) + text = ExpandTextVars( text, &sheetResolver, project ); + else + text = ExpandTextVars( text, nullptr, project ); + } } // WARNING: the IDs of FIELDS and SHEETS overlap, so one must check *both* the @@ -204,11 +208,7 @@ void SCH_FIELD::SwapData( SCH_ITEM* aItem ) const EDA_RECT SCH_FIELD::GetBoundingBox() const { // Calculate the text bounding box: - EDA_RECT rect; - SCH_FIELD text( *this ); // Make a local copy to change text - // because GetBoundingBox() is const - text.SetText( GetShownText() ); - rect = text.GetTextBox(); + EDA_RECT rect = GetTextBox(); // Calculate the bounding box position relative to the parent: wxPoint origin = GetParentPosition(); diff --git a/eeschema/sch_painter.cpp b/eeschema/sch_painter.cpp index cd8ffdb132..4d3e823d74 100644 --- a/eeschema/sch_painter.cpp +++ b/eeschema/sch_painter.cpp @@ -602,9 +602,9 @@ void SCH_PAINTER::draw( LIB_FIELD *aField, int aLayer ) COLOR4D color = getRenderColor( aField, aLayer, drawingShadows ); - if( !aField->IsVisible() ) + if( !( aField->IsVisible() || aField->IsForceVisible() ) ) { - if( m_schSettings.m_ShowHiddenText || aField->IsBrightened() ) + if( m_schSettings.m_ShowHiddenText ) color = getRenderColor( aField, LAYER_HIDDEN, drawingShadows ); else return; @@ -666,7 +666,7 @@ void SCH_PAINTER::draw( LIB_TEXT *aText, int aLayer ) if( !aText->IsVisible() ) { - if( m_schSettings.m_ShowHiddenText || aText->IsBrightened() ) + if( m_schSettings.m_ShowHiddenText ) color = getRenderColor( aText, LAYER_HIDDEN, drawingShadows ); else return; @@ -1265,9 +1265,9 @@ void SCH_PAINTER::draw( SCH_TEXT *aText, int aLayer ) if( conn && conn->IsBus() ) color = getRenderColor( aText, LAYER_BUS, drawingShadows ); - if( !aText->IsVisible() ) + if( !( aText->IsVisible() || aText->IsForceVisible() ) ) { - if( m_schSettings.m_ShowHiddenText || aText->IsBrightened() ) + if( m_schSettings.m_ShowHiddenText ) color = getRenderColor( aText, LAYER_HIDDEN, drawingShadows ); else return; @@ -1430,7 +1430,7 @@ void SCH_PAINTER::draw( SCH_FIELD *aField, int aLayer ) if( !( aField->IsVisible() || aField->IsForceVisible() ) ) { - if( m_schSettings.m_ShowHiddenText || aField->IsBrightened() ) + if( m_schSettings.m_ShowHiddenText ) color = getRenderColor( aField, LAYER_HIDDEN, drawingShadows ); else return; diff --git a/eeschema/sch_text.cpp b/eeschema/sch_text.cpp index 1544cd3e0f..f8b05b5127 100644 --- a/eeschema/sch_text.cpp +++ b/eeschema/sch_text.cpp @@ -509,15 +509,19 @@ wxString SCH_TEXT::GetShownText( int aDepth ) const return false; }; - PROJECT* project = nullptr; + bool processTextVars = false; + wxString text = EDA_TEXT::GetShownText( &processTextVars ); - if( g_RootSheet && g_RootSheet->GetScreen() ) - project = &g_RootSheet->GetScreen()->Kiway().Prj(); + if( processTextVars ) + { + PROJECT* project = nullptr; - wxString text = EDA_TEXT::GetShownText( aDepth ); + if( g_RootSheet && g_RootSheet->GetScreen() ) + project = &g_RootSheet->GetScreen()->Kiway().Prj(); - if( aDepth < 10 ) - text = ExpandTextVars( text, &textResolver, project ); + if( aDepth < 10 ) + text = ExpandTextVars( text, &textResolver, project ); + } return text; } diff --git a/include/eda_text.h b/include/eda_text.h index e3e5d768bf..6b36755c39 100644 --- a/include/eda_text.h +++ b/include/eda_text.h @@ -132,6 +132,17 @@ public: */ virtual wxString GetShownText( int aDepth = 0 ) const { return m_shown_text; } + /** + * A version of GetShownText() which also indicates whether or not the text needs + * to be processed for text variables. + * @param processTextVars [out] + */ + wxString GetShownText( bool* processTextVars ) const + { + *processTextVars = m_shown_text_has_text_var_refs; + return m_shown_text; + } + /** * Returns a shortened version (max 15 characters) of the shown text */ @@ -350,10 +361,11 @@ public: private: wxString m_text; - wxString m_shown_text; // Cache of unescaped text for efficient access + wxString m_shown_text; // Cache of unescaped text for efficient access + bool m_shown_text_has_text_var_refs; - TEXT_EFFECTS m_e; // Private bitflags for text styling. API above - // provides accessor funcs. + TEXT_EFFECTS m_e; // Private bitflags for text styling. API above + // provides accessor funcs. enum TE_FLAGS { TE_MIRROR, TE_ITALIC, diff --git a/pcbnew/class_pcb_text.cpp b/pcbnew/class_pcb_text.cpp index 819776efc8..0b2fdbdcb2 100644 --- a/pcbnew/class_pcb_text.cpp +++ b/pcbnew/class_pcb_text.cpp @@ -96,9 +96,10 @@ wxString TEXTE_PCB::GetShownText( int aDepth ) const return false; }; - wxString text = EDA_TEXT::GetShownText( aDepth ); + bool processTextVars = false; + wxString text = EDA_TEXT::GetShownText( &processTextVars ); - if( aDepth < 10 ) + if( processTextVars && aDepth < 10 ) text = ExpandTextVars( text, &pcbTextResolver, board->GetProject() ); return text; diff --git a/pcbnew/class_text_mod.cpp b/pcbnew/class_text_mod.cpp index 0141de1542..56e7e3300b 100644 --- a/pcbnew/class_text_mod.cpp +++ b/pcbnew/class_text_mod.cpp @@ -381,7 +381,7 @@ const BOX2I TEXTE_MODULE::ViewBBox() const double angle = GetDrawRotation(); EDA_RECT text_area = GetTextBox(); - if( angle ) + if( angle != 0.0 ) text_area = text_area.GetBoundingBoxRotated( GetTextPos(), angle ); return BOX2I( text_area.GetPosition(), text_area.GetSize() ); @@ -448,14 +448,19 @@ wxString TEXTE_MODULE::GetShownText( int aDepth ) const return module && module->ResolveTextVar( token, aDepth ); }; - PROJECT* project = nullptr; - wxString text = EDA_TEXT::GetShownText( aDepth ); + bool processTextVars = false; + wxString text = EDA_TEXT::GetShownText( &processTextVars ); + + if( processTextVars ) + { + PROJECT* project = nullptr; - if( module && module->GetParent() ) - project = static_cast( module->GetParent() )->GetProject(); + if( module && module->GetParent() ) + project = static_cast( module->GetParent() )->GetProject(); - if( aDepth < 10 ) - text = ExpandTextVars( text, &moduleResolver, project ); + if( aDepth < 10 ) + text = ExpandTextVars( text, &moduleResolver, project ); + } return text; }