-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Replace std::list with std::vector where appropriate #29827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
@juli27 Would you mind taking a look at this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! One minor comment and two nit-picky comments.
This speeds up score loading a bit (Beethoven 9th score from ~37s down to ~32s)
src/framework/global/containers.h
Outdated
inline bool contains_if(const std::vector<T>& vec, Predicate pred) | ||
{ | ||
return std::find_if(vec.cbegin(), vec.cend(), pred) != vec.cend(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: forwarding reference & forward to avoid copy. (std::function can heap allocate)
inline bool contains_if(const std::vector<T>& vec, Predicate pred) | |
{ | |
return std::find_if(vec.cbegin(), vec.cend(), pred) != vec.cend(); | |
inline bool contains_if(const std::vector<T>& vec, Predicate&& pred) | |
{ | |
return std::find_if(vec.cbegin(), vec.cend(), std::forward<Predicate>(pred)) != vec.cend(); |
src/engraving/dom/undo.cpp
Outdated
@@ -650,7 +653,7 @@ bool UndoMacro::empty() const | |||
|
|||
void UndoMacro::append(UndoMacro&& other) | |||
{ | |||
appendChildren(&other); | |||
appendChildren(std::forward<UndoMacro>(other)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty unusual and unsafe usage of rvalue references, because we state here that other
is moved into appendChildren
but then continue to use it further down this function. It works only because we don't actually move the entire UndoMacro
in appendChildren
but only the child commands vector. Let's get rid of the move iterator in appendChildren
, which only copies the raw pointers anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I pushed some code review fixes in a separate commit.
But with a std::vector range represented by a start and end iterator. Instead of popping from a list, we're now shrinking the vector range, until it's empty. Involves consistent usage of std::remove and std::remove_if, also with reverse iterators. std::remove[_if] are of course in theory a little bit slower than std::list::remove[_if], because they need to `std::move` some values around. But according to the literature, that cost is easily outweighed by the benefits of std::vector, like cache-friendliness.
There were a few `remove` calls, but not significant enough to warrant `std::list`, given that the lists here should be relatively small.
Co-Authored-By: Julian Bühler <[email protected]>
390437e
to
6855563
Compare
Resolves: #12219 (almost completely)