Simple-optimized-A-Pathfinder

August 27, 2020 ยท View on GitHub

Simple-optimized-A-Pathfinder

License: MIT

A simple pathfinder that I tried to optimize the maximum as I can, and share what I've discovered :)

The idea was pretty simple, implement a simple A* Pathfinder and use Unity Profiler to try to optimize the best as I can, trying to reduce the GC and the MS from the method itself

Final result:

alt text

Steps Descriptions

  • Step 1 - Dictionary used to for checking if a Tile is already on OpenList;
  • Step 2 - Caching index when comparing best tile from the open list, and using it to remove it from the list;
  • Step 3 - Static neighbours array;
  • Step 4 - Add FastPriorityQueue as OpenList;
  • Step 5 - Replacing Vector2Int to int for X and Y position;
  • Step 6 - Using reversed for instead of foreach on the neighbour's array;
  • Step 7 - Removing Dictionary for the open list, since FastQueue it's doing the same
  • Step 8 - Change return List to be List from Tile
  • Step 9 - Using only F cost when adding to the priority queue
  • Step 10 - Removing the reverse method from the GetPath
  • Step 11 - Removing closedList and using a Toggle inside the Tile itself
  • Step 12 - Increasing return list size to be 20% of the available tiles on the map

Performance Comparison

Keep in mind that all the results bellow are made inside Unity Deep Profiler, so the real performance its probably better than this, I'm working on the profiler for real builds

ActionGCGetPath() MSGC to originalGC to previousGetPath() MS to originalGetPath() MS ToPreviousCommit
Original40,6 KB29,87 msOriginal File
Step 150,3 KB20,14 ms+23,89%+23,89%-32,57%-32,57%Commit
Step 250,3 KB16,95 ms+23,89%0%-43,25%-15,87%Commit
Step 336,2 KB16,92 ms-10,14%-28,03%-43,35%-0,18%Commit
Step 432 KB10,97 ms-21,18%-11,60%-63,27%-35,17%Commit
Step 532 KB9,91 ms-21,18%0%-66,82%-9,66%Commit
Step 632 KB10,11 ms-21,18%0%-66,15%-2,02%Commit
Step 722,1 KB7,53 ms-45,57%-30,94%-74,79%-25,52%Commit
Step 822,1 KB7,46 ms-45,57%0%-75,03%-0,93%Commit
Step 912,6 KB7,46 ms-68,97%-42,99%-75,03%0%Commit
Step 1012,6 KB7,32 ms-68,97%0%-75,49%-1,88%Commit
Step 114,2 KB6,59 ms-89,66%-66,67%-77,94%-9,97%Commit
Step 120 KB4,82 ms-100%-100%-83,86%-26,86%Commit

profiler