From f9d4b757264b300c9a8a731ffc7f2f854ff32569 Mon Sep 17 00:00:00 2001 From: Mike Williams Date: Sat, 17 Jun 2023 12:56:00 -0400 Subject: [PATCH] Symbol Fields Table: performance optimizations Fixes: https://gitlab.com/kicad/code/kicad/-/issues/14972 --- .../dialogs/dialog_symbol_fields_table.cpp | 10 ++++++++- eeschema/fields_data_model.cpp | 22 +++++++++++++++++++ eeschema/fields_data_model.h | 8 ++++++- eeschema/sch_reference_list.h | 1 + 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/eeschema/dialogs/dialog_symbol_fields_table.cpp b/eeschema/dialogs/dialog_symbol_fields_table.cpp index 0b2411b7c9..7ba97d905c 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table.cpp @@ -1509,6 +1509,10 @@ void DIALOG_SYMBOL_FIELDS_TABLE::onBomPresetChanged( wxCommandEvent& aEvent ) void DIALOG_SYMBOL_FIELDS_TABLE::doApplyBomPreset( const BOM_PRESET& aPreset ) { + // Disable rebuilds while we're applying the preset otherwise we'll be + // rebuilding the model constantly while firing off wx events + m_dataModel->DisableRebuilds(); + // Basically, we apply the BOM preset to the data model and then // update our UI to reflect resulting the data model state, not the preset. m_dataModel->ApplyBomPreset( aPreset ); @@ -1580,6 +1584,7 @@ void DIALOG_SYMBOL_FIELDS_TABLE::doApplyBomPreset( const BOM_PRESET& aPreset ) // This will rebuild all rows and columns in the model such that the order // and labels are right, then we refresh the shown grid data to match + m_dataModel->EnableRebuilds(); m_dataModel->RebuildRows(); m_grid->ForceRefresh(); } @@ -1900,7 +1905,10 @@ void DIALOG_SYMBOL_FIELDS_TABLE::doApplyBomFmtPreset( const BOM_FMT_PRESET& aPre m_checkKeepTabs->SetValue( aPreset.keepTabs ); m_checkKeepLineBreaks->SetValue( aPreset.keepLineBreaks ); - PreviewRefresh(); + + // Refresh the preview if that's the current page + if( m_nbPages->GetSelection() == 1 ) + PreviewRefresh(); } diff --git a/eeschema/fields_data_model.cpp b/eeschema/fields_data_model.cpp index 1331e0eb56..6091a2a959 100644 --- a/eeschema/fields_data_model.cpp +++ b/eeschema/fields_data_model.cpp @@ -350,8 +350,23 @@ bool FIELDS_EDITOR_GRID_DATA_MODEL::groupMatch( const SCH_REFERENCE& lhRef, } +void FIELDS_EDITOR_GRID_DATA_MODEL::EnableRebuilds() +{ + m_rebuildsEnabled = true; +} + + +void FIELDS_EDITOR_GRID_DATA_MODEL::DisableRebuilds() +{ + m_rebuildsEnabled = false; +} + + void FIELDS_EDITOR_GRID_DATA_MODEL::RebuildRows() { + if( !m_rebuildsEnabled ) + return; + if( GetView() ) { // Commit any pending in-place edits before the row gets moved out from under @@ -379,6 +394,13 @@ void FIELDS_EDITOR_GRID_DATA_MODEL::RebuildRows() bool matchFound = false; + // Performance optimization for ungrouped case to skip the N^2 for loop + if( !m_groupingEnabled && !ref.IsMultiUnit() ) + { + m_rows.emplace_back( DATA_MODEL_ROW( ref, GROUP_SINGLETON ) ); + continue; + } + // See if we already have a row which this symbol fits into for( DATA_MODEL_ROW& row : m_rows ) { diff --git a/eeschema/fields_data_model.h b/eeschema/fields_data_model.h index c8fe052a71..b8b765b090 100644 --- a/eeschema/fields_data_model.h +++ b/eeschema/fields_data_model.h @@ -53,7 +53,7 @@ public: FIELDS_EDITOR_GRID_DATA_MODEL( SCH_REFERENCE_LIST& aSymbolsList ) : m_symbolsList( aSymbolsList ), m_edited( false ), m_sortColumn( 0 ), m_sortAscending( false ), m_groupingEnabled( false ), m_excludeDNP( false ), - m_includeExcluded( false ) + m_includeExcluded( false ), m_rebuildsEnabled( true ) { m_symbolsList.SplitReferences(); } @@ -131,7 +131,12 @@ public: int GetSortCol() { return m_sortColumn; } bool GetSortAsc() { return m_sortAscending; } + // These are used to disable the RebuildRows functionality while we're generating + // lots of events in the UI, e.g. applying a BOM preset, that would thrash the grid. + void EnableRebuilds(); + void DisableRebuilds(); void RebuildRows(); + void ExpandRow( int aRow ); void CollapseRow( int aRow ); void ExpandCollapseRow( int aRow ); @@ -204,6 +209,7 @@ protected: bool m_groupingEnabled; bool m_excludeDNP; bool m_includeExcluded; + bool m_rebuildsEnabled; std::vector m_cols; std::vector m_rows; diff --git a/eeschema/sch_reference_list.h b/eeschema/sch_reference_list.h index 93c3c6889e..8bd49e06c7 100644 --- a/eeschema/sch_reference_list.h +++ b/eeschema/sch_reference_list.h @@ -100,6 +100,7 @@ public: int GetUnit() const { return m_unit; } void SetUnit( int aUnit ) { m_unit = aUnit; } + bool IsMultiUnit() const { return GetLibPart()->GetUnitCount() > 1; } const wxString GetValue() const { return m_value; } void SetValue( const wxString& aValue ) { m_value = aValue; }