Skip to content

Optimize unionfind cache locality. - #438

Merged
christian-rauch merged 1 commit into
AprilRobotics:masterfrom
openmv:optimize_unionfind
Apr 17, 2026
Merged

Optimize unionfind cache locality.#438
christian-rauch merged 1 commit into
AprilRobotics:masterfrom
openmv:optimize_unionfind

Conversation

@iabdalkader

@iabdalkader iabdalkader commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

This patch interleaves node parent and size to improve cache locality. With this, accessing parent[i] and size[i] hits the same cache line instead of arrays that are MBs apart. Note this change wouldn't make any difference on machines with MBs of cache, however on MCUs with a few KBs of cache, it shows a significant difference.

I've tested this on both real hardware (STMN6 Cortex-M55) and Arm's FVP (Fixed Virtual Platform) by running AprilTag on one of the test images 100 times and averaging run times. The results show ~5-6% speedup:

# N6/CM55
find_apriltags: 71473 us avg (100 runs)  # before
find_apriltags: 67636 us avg (100 runs) # after

# FVP/CM55
find_apriltags: 826046 us avg (100 runs)  # before
find_apriltags: 772852 us avg (100 runs)  # after

This patch interleaves node parent and size to improve cache locality.
With this, accessing parent[i] and size[i] hits the same cache line
instead of arrays that are MBs apart arrays.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
@christian-rauch
christian-rauch merged commit 02e18fa into AprilRobotics:master Apr 17, 2026
42 checks passed
@iabdalkader
iabdalkader deleted the optimize_unionfind branch April 17, 2026 15:46
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.

2 participants