Don't resize vectors in hot loops

Fixes a slowdown caused by erasing elements from the middle of a vector
during a hot loop in SHAPE_LINE_CHAIN::Simplify().  This gets called
quite a bit when loading boards and updating lines, so it needs to be as
fast as possible

Fixes https://gitlab.com/kicad/code/kicad/issues/12115
This commit is contained in:
Seth Hillbrand 2022-07-31 12:46:37 -07:00
parent 650caae323
commit c9c31fcbc2
1 changed files with 8 additions and 11 deletions

View File

@ -1742,6 +1742,9 @@ SHAPE_LINE_CHAIN& SHAPE_LINE_CHAIN::Simplify( bool aRemoveColinear )
i = 0;
m_points.push_back( pts_unique[0] );
m_shapes.push_back( shapes_unique[0] );
// stage 2: eliminate colinear segments
for( i = 1 ; i < np - 1 ; i ++ )
{
@ -1756,22 +1759,16 @@ SHAPE_LINE_CHAIN& SHAPE_LINE_CHAIN::Simplify( bool aRemoveColinear )
const auto distToMidpoint = SEG( p_prev, p_next ).LineDistance( midpoint );
const auto isMidpointColinear = SEG( p_prev, p_next ).Collinear( SEG( p_prev, midpoint ) );
if( distToMidpoint <= 1 || isMidpointColinear )
if( distToMidpoint > 1 && !isMidpointColinear )
{
pts_unique.erase( pts_unique.begin() + i );
shapes_unique.erase( shapes_unique.begin() + i );
i--;
np--;
continue;
m_points.push_back( pts_unique[i] );
m_shapes.push_back( shapes_unique[i] );
}
}
}
for( i = 0 ; i < np; i ++ )
{
m_points.push_back( pts_unique[i] );
m_shapes.push_back( shapes_unique[i] );
}
m_points.push_back( pts_unique.back() );
m_shapes.push_back( shapes_unique.back() );
assert( m_points.size() == m_shapes.size() );