lcModel::AddPiece is slow when importing large models
The O(N) lookup in this code has a high performance cost for models with many pieces. If there are N calls, this takes O(N^2) time. Removing the lookup creates a dramatic improvement to import times. Is the goal here to sort mPieces in ascending order by the value returned by GetStepShow()? This could use something like std::map to make the lookup operation O(log(N)) instead of O(N). The overall cost would be O(N log(N)) and scale much better than the current implementation.
https://github.com/leozide/leocad/blob/697e89b1290bcb244b3c17feb1a9420f707132ad/common/lc_model.cpp#L2182-L2194
When loading file, a line with content 0 STEP, increases the step number, so normally pieces has to be inserted at the end. So this modified AddPiece, tries to see weather a piece can be inserted at the end of the list:
void lcModel::AddPiece(lcPiece* Piece)
{
if(mPieces.GetSize() > 0 && Piece->GetStepShow() >= mPieces[mPieces.GetSize() - 1]->GetStepShow()) {
InsertPiece(Piece, mPieces.GetSize());
}
else {
for (int PieceIdx = 0; PieceIdx < mPieces.GetSize(); PieceIdx++)
{
if (mPieces[PieceIdx]->GetStepShow() > Piece->GetStepShow())
{
InsertPiece(Piece, PieceIdx);
return;
}
}
InsertPiece(Piece, mPieces.GetSize());
}
}
On my computer it decreases the load time from 430 seconds to below 4 seconds :-).
But I still only get a black screen, so something else has to be changed 🧐