From 6e572661367b63b39e17b67410d8a133c0694ea4 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sat, 2 Jun 2018 18:14:34 +0200 Subject: [PATCH] Pcbnew: optimize BOARD::SortedNetnamesList() for a must faster calculation time. It is mainly used in copper zones dialog editor, and previously created a noticeable delay to show this dialog with large boards (more than 900 nets) --- pcbnew/class_board.cpp | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/pcbnew/class_board.cpp b/pcbnew/class_board.cpp index 1371c56ff3..ab7b9a7bc9 100644 --- a/pcbnew/class_board.cpp +++ b/pcbnew/class_board.cpp @@ -6,11 +6,11 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2017 Jean-Pierre Charras, jp.charras at wanadoo.fr + * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck * Copyright (C) 2011 Wayne Stambaugh * - * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -1388,12 +1388,17 @@ MODULE* BOARD::FindModule( const wxString& aRefOrTimeStamp, bool aSearchByTimeSt } -// Sort nets by decreasing pad count. For same pad count, sort by alphabetic names + +// The pad count for each netcode, stored in a buffer for a fast access. +// This is needed by the sort function sortNetsByNodes() +static std::vector padCountListByNet; + +// Sort nets by decreasing pad count. +// For same pad count, sort by alphabetic names static bool sortNetsByNodes( const NETINFO_ITEM* a, const NETINFO_ITEM* b ) { - auto connectivity = a->GetParent()->GetConnectivity(); - int countA = connectivity->GetPadCount( a->GetNet() ); - int countB = connectivity->GetPadCount( b->GetNet() ); + int countA = padCountListByNet[a->GetNet()]; + int countB = padCountListByNet[b->GetNet()]; if( countA == countB ) return a->GetNetname() < b->GetNetname(); @@ -1426,7 +1431,25 @@ int BOARD::SortedNetnamesList( wxArrayString& aNames, bool aSortbyPadsCount ) // sort the list if( aSortbyPadsCount ) + { + // Build the pad count by net: + padCountListByNet.clear(); + std::vector pads = GetPads(); + + // Calculate the max value of net codes, and creates the buffer to + // store the pad count by net code + int max_netcode = 0; + + for( D_PAD* pad : pads ) + max_netcode = std::max( max_netcode, pad->GetNetCode() ); + + padCountListByNet.assign( max_netcode+1, 0 ); + + for( D_PAD* pad : pads ) + padCountListByNet[pad->GetNetCode()]++; + sort( netBuffer.begin(), netBuffer.end(), sortNetsByNodes ); + } else sort( netBuffer.begin(), netBuffer.end(), sortNetsByNames );