Skip to content

Optimize vector warp interpolation#3274

Merged
daljit46 merged 3 commits intodevfrom
optimise_warp_interp
Mar 19, 2026
Merged

Optimize vector warp interpolation#3274
daljit46 merged 3 commits intodevfrom
optimise_warp_interp

Conversation

@daljit46
Copy link
Member

@daljit46 daljit46 commented Mar 16, 2026

Profiling mrregister on macOS showed that the hottest symbol was MR::Interp::LinearInterp<...>::row(unsigned long) called from Registration::Warp::DisplacementThreadKernel::invert_displacement(). That generic interpolation path builds a dynamic Eigen matrix for each sample, which is often unnecessary for vector fields using 3 component vectors.
This change the implementation of LinearInterp::row() to avoid constructing an 3x8 matrix and instead does the weighted summation inside the for loop. Additionally, a new vec3() wrapper is added to avoid dynamic allocation for the common case of interpolating 3-volume vector fields. This helps reducing temporary allocations while preserving the existing interpolation behavior.

In my tests, this doesn't yield any noticeable performance gains on Linux, but on macOS there seems to be a consistent 15-20% performance improvement when running scalar registration of 3D volumes.

Profiling mrregister on macOS showed that the hottest symbol was 
`MR::Interp::LinearInterp<...>::row(unsigned long)` called from
`Registration::Warp::DisplacementThreadKernel::invert_displacement()`.
That generic interpolation path builds a dynamic Eigen matrix for each 
sample, which is often unnecessary for vector fields using 3 component 
vectors. 
This change the implementation of `LinearInterp::row()` to avoid 
constructing an 3x8 matrix and instead does the weighted summation 
inside the for loop. Additionally, a new vec3() wrapper is added to 
avoid dynamic allocation for the common case of interpolating 3-volume 
vector fields. This helps reducing temporary allocations while 
preserving the existing interpolation behavior.
@daljit46 daljit46 requested a review from a team March 16, 2026 10:47
@daljit46 daljit46 self-assigned this Mar 16, 2026
@github-actions
Copy link

clang-tidy review says "All clean, LGTM! 👍"

@github-actions
Copy link

clang-tidy review says "All clean, LGTM! 👍"

@Lestropie
Copy link
Member

Left a couple of comments.

I wonder if there might be other places in the code base where .row(3) could be similarly replaced with .vec3(3). A simple grep -R gives a few candidates, but without checking whether the object being accessed is an interpolator it's hard to say. I might be able to have a look later (I really need to learn git worktree...).

@github-actions
Copy link

clang-tidy review says "All clean, LGTM! 👍"

@daljit46 daljit46 merged commit d603261 into dev Mar 19, 2026
6 checks passed
@daljit46 daljit46 deleted the optimise_warp_interp branch March 19, 2026 09:45
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.

2 participants