Skip to content

Conversation

@bobtista
Copy link

Refactors path-following functions to use move when transferring std::vector<Coord3D> ownership

Changes:

  • Updated aiFollowExitProductionPath and aiFollowPath inline functions to accept non-const vector pointers and use std::move
  • Modified AIStateMachine::setGoalPath to move the vector instead of copying
  • Updated privateFollowPath and doQuickExit signatures to support move semantics

@bobtista bobtista marked this pull request as ready for review November 25, 2025 17:58
Copy link

@xezon xezon left a 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?

#else
// TheSuperHackers @performance bobtista 23/11/2025 Use swap to emulate move semantics for VC6 compatibility
parms.m_coords.swap(*path);
path->clear();
Copy link

Choose a reason for hiding this comment

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

This clear() is redundant. path will already be empty.

Choose a reason for hiding this comment

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

path will already be empty.

Why would it be?

Copy link

Choose a reason for hiding this comment

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

Because AICommandParms parms(...) is created with empty m_coords.

Choose a reason for hiding this comment

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

I think making the code clearer at a 'local' level can't hurt, but this is now probably a moot point with a swap / move helper function.

Copy link

Choose a reason for hiding this comment

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

You are not wrong, it is reasonable to make the intent clear, but in that case a comment would be better then instead of adding a redundant function call.

AICommandParms parms(AICMD_FOLLOW_PATH, cmdSource);
parms.m_coords = *path;
#if __cplusplus >= 201103L
parms.m_coords = std::move(*path);
Copy link

Choose a reason for hiding this comment

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

Keep it simple and use swap even for c++11

Copy link

@Caball009 Caball009 Nov 25, 2025

Choose a reason for hiding this comment

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

Swap is unnecessary here and does not convey the intended operation: a single move operation; we don't care about the contents (if anything) of parms.m_coords.

Copy link

Choose a reason for hiding this comment

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

Yes but then we do not need all this #if __cplusplus >= 201103L stuff here.

Otherwise, create a "move" helper somewhere else that we use here, which does move in c++11 and swap in C++98.

Copy link
Author

Choose a reason for hiding this comment

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

Ok I added a helper to CppMacros.h

Copy link

Choose a reason for hiding this comment

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

Ok that works. I think CppMacros.h needs renaming to to cpp_compat.h later :-)

@xezon xezon added Minor Severity: Minor < Major < Critical < Blocker Performance Is a performance concern labels Nov 25, 2025
@bobtista
Copy link
Author

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?

@xezon
Copy link

xezon commented Nov 25, 2025

Yes, when the std::vector contents are not used anymore after they are now consumed by the functions then this is safe.


// TheSuperHackers @performance bobtista 25/11/2025 Helper to move-assign from pointer: uses std::move in C++11, swap in C++98
template<typename T>
inline void move_assign_from_pointer(T& dest, T* src)
Copy link

Choose a reason for hiding this comment

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

T& src


// TheSuperHackers @performance bobtista 25/11/2025 Helper to move-assign from pointer: uses std::move in C++11, swap in C++98
template<typename T>
inline void move_assign_from_pointer(T& dest, T* src)
Copy link

Choose a reason for hiding this comment

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

move_or_swap

#include "GameLogic/Damage.h"
#include "Common/STLTypedefs.h"
#include "ref_ptr.h"
#include "Utility/CppMacros.h"
Copy link

Choose a reason for hiding this comment

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

This include should not be necessary. It should already be included by other means.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Minor Severity: Minor < Major < Critical < Blocker Performance Is a performance concern

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider moving std::vector instead of copying the data

3 participants