Sped up pose estimation by reducing allocations and string parse in matd_op#445
Open
ckorris-imt wants to merge 2 commits into
Open
Sped up pose estimation by reducing allocations and string parse in matd_op#445ckorris-imt wants to merge 2 commits into
ckorris-imt wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
My friend/colleague @jonlinsner and I found that
estimate_tag_posewas much slower than expected - over a millisecond per pose on my work desktop. This was because of a lot of allocation that happens when callingmatd_op, due to parsing a string each call, andmatd_create, which makes new matrices. That means that every intermediate 3x3 or 3x1 result in the process (which runs iteratively in a loop) results in temporary heap allocations - about 26,000 per pose.This PR rewrites the hot path (
orthogonal_iteration,fix_pose_ambiguities,calculate_F) to do the same math on fixed-size, stack-allocated values. In an attempt to be consistent with existing code, I added utilities incommon/svd33to mirror howmatd_svdusescommon/svd22.I had Claude build harnesses to test the before and after using 20,000 randomized synthetic detections (changing poses, focal lengths, tag size, and sub-pixel corner noise) and found that the worst-case difference was ~9e-11 for rotation and ~6e-13 for translation.
On my work PC, the cost to run
estimate_tag_poseonce went from ~1.86ms to ~0.08ms.In full disclosure, Claude generated the corrected code, as it's a much better mathematician than I. I supervised it closely to make it consistent with the rest of the code base, and to make sure tests were thorough. It actually found an additional place for performance (which is noted in a comment) which reduced it by about ~30 µs more, but I wanted the code to be closer to your existing, tried-and-true math. If this PR is accepted, I'll do more tests to make sure there's no accuracy regression and open a second PR to address that.
In addition to the test harness, I logged the time it takes to detect one tag from within my Unity application. It's about 10x faster, including some extra overhead (but also including the second, much smaller performance improvement I've deferred for now).
For verifying, the test harnesses can be found here with instructions.