skell wrote:
@TFM: I also included your optimization suggestions. There was another issue - it would add the same orb multiple times to the vector and merge the orb once for each agent.
Hmm... well, the main optimization issue you're running into is that you're iterating over the agents far too many times.
EDIT: To be clear here, I'd just been concentrating on MergeOrbConnections when I was looking at the code originally, so I thought you were looking at each orb a maximum of twice. I hadn't realised there was already another iteration in GetOrbConnections, so I'm addressing that now in this post.
The code looks like it's doing something like this:
*
GetOrbConnections: Iterate through all orbs in the room. Iterate through all agents in each orb. If agent connects to merged door, mark orb for merging. (With the PR, this now only marks an orb if 2 or more agents connect to merged door.)
*
MergeOrbConnections Part 1: Iterate through all agents in a marked orb. Collect information about each action of the agent if it points to merged door. If at least one action is found, move to Part 2.
*
MergeOrbConnections Part 2: Iterate through all agents in a marked orb. If it doesn't point to the merged door, ignore it. If it's the first action found that does so, edit the action to whatever was decided in Part 1; if it's not the first action, delete it.
So each orb is being iterated through at least once, and merged orbs are being looked through up to three times.
Instead, you could drop GetOrbConnections, and do something like this:
*
MergeOrbConnections Part 1: Iterate through all agents in an orb (marked or not). Collect information about each action of the agent if it points to merged door. Ignore actions that don't point to merged door. If it's the first action found, keep track of it. If it's not the first action but still must be merged, delete it.
*
MergeOrbConnections Part 2: Edit the first action found with the combined action we calculated in Part 1.
This way, each orb is iterated over once only, merged actions are removed immediately, and orbs that have no actions that need to be merged are already ignored.
[Last edited by TFMurphy at 11-08-2020 05:25 PM]