Skip to content

Conversation

cbjeukendrup
Copy link
Member

@cbjeukendrup cbjeukendrup commented Sep 7, 2025

@cbjeukendrup
Copy link
Member Author

@juli27 Could you please review this?

Copy link
Contributor

@juli27 juli27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cleaner (but a bit more involved) fix would be to rewrite the logic in terms of std::remove_if. But this is fine too (once it compiles with GCC 10)

Did not know that reverse iterators worked differently. What fun!

@cbjeukendrup
Copy link
Member Author

Thanks for the hint about static_assert(false)! Will apply that.

About std::remove_if, note that we're working with std::list here, for which std::remove_if is not efficient; and std::list::remove_if doesn't take iterators as arguments. So unless std::remove_if is specialised for std::list iterators, which it isn't as far I know, I think this custom logic is the best we can get, right?

Co-authored-by: Julian Bühler <[email protected]>
@juli27
Copy link
Contributor

juli27 commented Sep 8, 2025

TLDR: The best for performance would be to replace the list with a vector. But short of that, yes, the custom logic is better.

std::list is bad for performance in general. (see this FAQ entry by Bjarne Stroustrup)

My suggestion is:

  • don't store data unnecessarily,
  • keep data compact, and
  • access memory in a predictable manner.

I emphasize the importance of cache effects. In my experience, all but true experts tend to forget those when algorithms are discussed.
And, yes, my recomendation is to use std::vector by default. More generally, use a contiguous representation unless there is a good reason not to.

The performance hit of visiting each element far outweigh the (often) trivial costs of moving elements around in a vector caused by removing. (especially when the contents can be std::memcpyd)

@cbjeukendrup cbjeukendrup marked this pull request as ready for review September 8, 2025 19:41
@cbjeukendrup cbjeukendrup merged commit f37bf9f into musescore:master Sep 8, 2025
13 checks passed
@cbjeukendrup cbjeukendrup deleted the iterator_fix branch September 8, 2025 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants