-
Notifications
You must be signed in to change notification settings - Fork 120
Use move for std::vector instead of copying in path functions #1895
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: main
Are you sure you want to change the base?
Use move for std::vector instead of copying in path functions #1895
Conversation
xezon
left a comment
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.
Did you check that the passed vector is never used after it is consumed?
Looking at call sites, they all pass local vectors (exitPath, path) that are not used after the function call. They go out of scope immediately, so this is safe, right? |
|
Yes, when the std::vector contents are not used anymore after they are now consumed by the functions then this is safe. |
| break; | ||
| case AICMD_FOLLOW_PATH: | ||
| privateFollowPath(&parms->m_coords, parms->m_obj, parms->m_cmdSource, FALSE); | ||
| privateFollowPath(&const_cast<AICommandParms*>(parms)->m_coords, parms->m_obj, parms->m_cmdSource, FALSE); |
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.
What is going on with the const_cast here? This looks dangerous. Can we get rid of it?
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.
Yeah, it can be mutable. Another option maybe we change aiDoCommand to take non-const pointer, but that changes more stuff. I set it to mutable.
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.
That is not so elegant either.
The problem looks to be that AICommandParms is passed as const to aiDoCommand functions. I suspect we need another change and pass it as non const, UNLESS there is a very good reason why we cannot consume these structures.
This would also allow to optimize this call:
m_pendingCommand.store(*parms)
It currently creates a copy of the whole thing, instead of swapping/moving it.
Perhaps we need another change, refactoring aiDoCommand first, then optimize m_pendingCommand and this change.
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.
#1924, and I used a script to replicate to generals :)
| break; | ||
| case AICMD_FOLLOW_PATH: | ||
| privateFollowPath(&parms->m_coords, parms->m_obj, parms->m_cmdSource, FALSE); | ||
| privateFollowPath(&const_cast<AICommandParms*>(parms)->m_coords, parms->m_obj, parms->m_cmdSource, FALSE); |
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.
That is not so elegant either.
The problem looks to be that AICommandParms is passed as const to aiDoCommand functions. I suspect we need another change and pass it as non const, UNLESS there is a very good reason why we cannot consume these structures.
This would also allow to optimize this call:
m_pendingCommand.store(*parms)
It currently creates a copy of the whole thing, instead of swapping/moving it.
Perhaps we need another change, refactoring aiDoCommand first, then optimize m_pendingCommand and this change.
Refactors path-following functions to use move when transferring
std::vector<Coord3D>ownershipChanges:
aiFollowExitProductionPathandaiFollowPathinline functions to accept non-const vector pointers and usestd::moveAIStateMachine::setGoalPathto move the vector instead of copyingprivateFollowPathanddoQuickExitsignatures to support move semantics