Skip to content
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

Replace some distance checks with square distance checks in NavMap #93005

Merged
merged 1 commit into from
Oct 17, 2024

Conversation

MajorMcDoom
Copy link
Contributor

Replaced some distance checks with square distance checks in NavMap, wherever the purpose is only to find the nearest element. Some of these checks can be made thousands of times a frame for path finding, so this should cut down on some math costs. Places where the distance is actually used in some sort of scoring formula were left alone.

@smix8
Copy link
Contributor

smix8 commented Jun 13, 2024

Would like to see some performance tests, the change looks sound on paper.

Tried with this PR and master and to my surprise I get 2-3 fps on average less with 1000 agents on an unoptimized dev build with this PR and I have no explanation why.

@MajorMcDoom MajorMcDoom force-pushed the sqr-dist-optimization branch 2 times, most recently from 819d8fe to 621fbc9 Compare July 2, 2024 19:38
@MajorMcDoom
Copy link
Contributor Author

I just did a test with this project:
nav-sqr-dist-test.zip

Results:

[BIG MAP, SINGLE REGION] -------------------------------------------------------

# WITHOUT sqr_dist
map_get_closest_point_to_segment (intersecting): 2547.928 ms
map_get_closest_point_to_segment (non-intersecting): 3510.255 ms
map_get_closest_point_to_segment (intersecting collision): 672.153 ms
map_get_closest_point_to_segment (non-intersecting collision): 467.859 ms
map_get_path (non-optimized): 5577.937 ms
map_get_path (optimized): 5565.387 ms

# WITH sqr_dist
map_get_closest_point_to_segment (intersecting): 2445.945 ms
map_get_closest_point_to_segment (non-intersecting): 3373.6 ms
map_get_closest_point_to_segment (intersecting collision): 659.314 ms
map_get_closest_point_to_segment (non-intersecting collision): 444.546 ms
map_get_path (non-optimized): 5444.421 ms
map_get_path (optimized): 5470.347 ms

[BIG MAP, MULTI REGION] --------------------------------------------------------

# WITHOUT sqr_dist
map_get_closest_point_to_segment (intersecting): 2247.26 ms
map_get_closest_point_to_segment (non-intersecting): 2892.012 ms
map_get_closest_point_to_segment (intersecting collision): 545.571 ms
map_get_closest_point_to_segment (non-intersecting collision): 377.858 ms
map_get_path (non-optimized): 2967.82 ms
map_get_path (optimized): 3012.839 ms

# WITH sqr_dist
map_get_closest_point_to_segment (intersecting): 2186.209 ms
map_get_closest_point_to_segment (non-intersecting): 2794.83 ms
map_get_closest_point_to_segment (intersecting collision): 547.577 ms
map_get_closest_point_to_segment (non-intersecting collision): 365.616 ms
map_get_path (non-optimized): 2918.6 ms
map_get_path (optimized): 2931.453 ms

[SMALL MAP] --------------------------------------------------------------------

# WITHOUT sqr_dist
map_get_closest_point_to_segment (intersecting): 3904.244 ms
map_get_closest_point_to_segment (non-intersecting): 3585.282 ms
map_get_closest_point_to_segment (intersecting collision): 693.023 ms
map_get_closest_point_to_segment (non-intersecting collision): 477.006 ms
map_get_path (non-optimized): 4051.911 ms
map_get_path (optimized): 4090.907 ms

# WITH sqr_dist
map_get_closest_point_to_segment (intersecting): 3789.921 ms
map_get_closest_point_to_segment (non-intersecting): 3418.402 ms
map_get_closest_point_to_segment (intersecting collision): 687.428 ms
map_get_closest_point_to_segment (non-intersecting collision): 468.693 ms
map_get_path (non-optimized): 3929.711 ms
map_get_path (optimized): 4041.489 ms

There definitely is an improvement, but it's also tiny.
I ran lots of tests, and while there are super rare outliers where the sqr dist performance was worse, overall it is about 2%-5% faster. This is a tiny gain, which is a little unexpected, but maybe my test isn't capturing the correct hot paths?

Based on these results, I see no reason not to merge this, but at the same time, it's not a super compelling reason to merge either.

@smix8 smix8 modified the milestones: 4.x, 4.4 Sep 22, 2024
Copy link
Contributor

@smix8 smix8 left a comment

Choose a reason for hiding this comment

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

Approve for the change in general. This still needs someone code review and a rebase.

Rebase because some of the query functions have been moved to the nav_mesh_queries_3d.h file.

I had time to test this PR more thoroughly with current master. Running 1000 iterations the results are on average at least the same, but for the more important hot paths like the main path function always better, even if only for a tiny amount. So I guess this is good.

Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

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

Code seems good to me.

Needs rebase. Be sure to notify us once it's done, as GitHub doesn't always send notifications for rebases for some reason.

@smix8
Copy link
Contributor

smix8 commented Oct 15, 2024

@MajorMcDoom Would you be available to rebase your PR? If not np, just let us know.

@MajorMcDoom
Copy link
Contributor Author

@MajorMcDoom Would you be available to rebase your PR? If not np, just let us know.

Hi! I will do it today. Thanks for the reminder.

…wherever the purpose is only to find the nearest element.
@MajorMcDoom MajorMcDoom force-pushed the sqr-dist-optimization branch from 621fbc9 to db194f0 Compare October 16, 2024 03:06
@MajorMcDoom
Copy link
Contributor Author

@smix8 @akien-mga
Rebased. Please let me know if I did the conflict resolution correctly, I'm still a bit new to this aspect of Git. Thanks!

@clayjohn clayjohn merged commit c6b94ca into godotengine:master Oct 17, 2024
20 checks passed
@clayjohn
Copy link
Member

Thank you!

@MajorMcDoom MajorMcDoom deleted the sqr-dist-optimization branch December 18, 2024 02:51
@MajorMcDoom MajorMcDoom restored the sqr-dist-optimization branch December 18, 2024 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants