Skip to content

Sped up pose estimation by reducing allocations and string parse in matd_op#445

Open
ckorris-imt wants to merge 2 commits into
AprilRobotics:masterfrom
ckorris-imt:pose-fixed-size-svd
Open

Sped up pose estimation by reducing allocations and string parse in matd_op#445
ckorris-imt wants to merge 2 commits into
AprilRobotics:masterfrom
ckorris-imt:pose-fixed-size-svd

Conversation

@ckorris-imt

Copy link
Copy Markdown

My friend/colleague @jonlinsner and I found that estimate_tag_pose was much slower than expected - over a millisecond per pose on my work desktop. This was because of a lot of allocation that happens when calling matd_op, due to parsing a string each call, and matd_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 in common/svd33 to mirror how matd_svd uses common/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_pose once 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.

@ckorris-imt ckorris-imt changed the title Sped up pose detection by reducing allocations and string parse in matd_op Sped up pose estimation by reducing allocations and string parse in matd_op Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant