Skip to content

Vectorize _on_minor_arc_xyz for improved large-data processing performance #1623

Closed

Description

@hongyuchen1030

The current implementation still involves some branching. Given this is one of our computation hot paths — every edge in a hundred-GB mesh passes through it — vectorizing this function should meaningfully improve large-data processing, and it's also a prerequisite for our planned GPU and multithreading support.

Modern compilers will optimize branches, but that doesn't mean the result is optimized for vectorization — these are two different problems. The compiler-optimizes-branches argument (from that old shader blog and the Jason Booth rebuttal) is about a single function processing one item at a time.

Vectorization is a different thing entirely: instead of computing one edge's answer at a time, we want to hand the CPU (or GPU, or a batch of threads) a whole array of edges — say 8, or 10,000 — and have it compute all their answers in one shot, using the exact same formula applied uniformly across all of them. That only works if the formula has no per-item decision-making in it.

See [Avoiding Shader Conditionals](https://theorangeduck.com/page/avoiding-shader-conditionals) for how boolean masking replaces if-else branching with plain arithmetic.

Activity

  1. added this to the Remapping milestone on Jul 25, 2026
  2. added
    improvement
    on Jul 25, 2026
  3. moved this from 📚 Backlog to 📝 To-Do in UXarray Developmenton Jul 25, 2026
  4. self-assigned this
    on Jul 27, 2026
  5. added
    scalability
    on Jul 27, 2026
  6. cmdupuis3 commented on Jul 27, 2026

    @cmdupuis3
    Collaborator

    I'll add this to my portfolio of optimization work.

  7. rajeeja commented on Jul 27, 2026

    @rajeeja
    Contributor

    I'll add this to my portfolio of optimization work.

    cool, thanks! while you are at it scan for more stuff like this.

  8. hongyuchen1030 commented on Jul 27, 2026

    @hongyuchen1030
    ContributorAuthor

    The current implementation still involves some branching. Given this is one of our computation hot paths — every edge in a hundred-GB mesh passes through it — vectorizing this function should meaningfully improve large-data processing, and it's also a prerequisite for our planned GPU and multithreading support.

    Modern compilers will optimize branches, but that doesn't mean the result is optimized for vectorization — these are two different problems. The compiler-optimizes-branches argument (from that old shader blog and the Jason Booth rebuttal) is about a single function processing one item at a time.

    Vectorization is a different thing entirely: instead of computing one edge's answer at a time, we want to hand the CPU (or GPU, or a batch of threads) a whole array of edges — say 8, or 10,000 — and have it compute all their answers in one shot, using the exact same formula applied uniformly across all of them. That only works if the formula has no per-item decision-making in it.

    See [Avoiding Shader Conditionals](https://theorangeduck.com/page/avoiding-shader-conditionals) for how boolean masking replaces if-else branching with plain arithmetic.

    Another note: What's implemented in AccuSphGeom's on_minor_arc.hpp, [line 43](https://github.com/hongyuchen1030/AccuSphGeom/blob/90b76b2e68220c15dbe1c85c00b92b385da19624/include/accusphgeom/predicates/on_minor_arc.hpp#L43) is itself a true branch-free implementation. It relies on the fact that a boolean maps to 1/0, so multiplication, XOR, and OR can select values without any if-else. So figuring out this issues should be pretty straightforward by simply matching the C++ AcuSphGeom implementation

  9. cmdupuis3 commented on Jul 27, 2026

    @cmdupuis3
    Collaborator

    @hongyuchen1030 Yeah, I did notice that. On testing though, the behavior of doing math with booleans in python is somewhat non-intuitive compared to C++, so we should be careful that the implementation results in the best speedup.

  10. hongyuchen1030 commented on Jul 28, 2026

    @hongyuchen1030
    ContributorAuthor

    @hongyuchen1030 Yeah, I did notice that. On testing though, the behavior of doing math with booleans in python is somewhat non-intuitive compared to C++, so we should be careful that the implementation results in the best speedup.

    That make sense, different compiler has different behavior when it comes to boolean. int should probably have a more steady behavior.

  11. Sevans711 commented on Aug 19, 2026

    @Sevans711
    Collaborator

    Clarifying, is this still an issue after #1513, and and is this still the desired route? Recent discussion explored vectorization versus numba, finding numba wins (at least for ragged arrays) when properly optimized, see e.g. #1566 (comment) and comments below it.

    Additionally, I noticed that many of the functions that currently utilize _on_minor_arc_xyz are constructing many tiny numpy arrays; converting to tuples instead would also cause a huge speedup as explored in issue 1566 and further clarified in #1648.

    Can you give one example of a public/common function which is slow and you would expect should be sped up by improving the efficiency here? I may be able to work on this as a sub-issue of issue 1648.

  12. cmdupuis3 commented on Aug 19, 2026

    @cmdupuis3
    Collaborator

    @Sevans711 I was working on this yesterday, and from what I gathered, it seems like any potential issues would be a level up from _on_minor_arc_xyz, so that tracks. I haven't gotten any major wins yet though, so I'll post what I find here if I get anything new out of it.

  13. cmdupuis3 commented on Aug 19, 2026

    @cmdupuis3
    Collaborator

    After investigating this, my conclusion is that _on_minor_arc_xyz is as vectorized as it can get. Porting the conditionals to boolean arithmetic results in the same IR for this python code, so my guess is that it's a feature of LLVM that doesn't necessarily happen with C++ compilers. With that, we don't need to worry about using boolean arithmetic here, because the results will be the same.

    However, there are opportunities to better vectorize the usage of _on_minor_arc_xyz where it's called.

  14. erogluorhan commented on Aug 19, 2026

    @erogluorhan
    Member

    After investigating this, my conclusion is that _on_minor_arc_xyz is as vectorized as it can get. Porting the conditionals to boolean arithmetic results in the same IR for this python code, so my guess is that it's a feature of LLVM that doesn't necessarily happen with C++ compilers. With that, we don't need to worry about using boolean arithmetic here, because the results will be the same.

    However, there are opportunities to better vectorize the usage of _on_minor_arc_xyz where it's called.

    That said, should this issue be closed as unplanned?

  15. cmdupuis3 commented on Aug 19, 2026

    @cmdupuis3
    Collaborator

    I think that makes sense, but I'll add a new issue for what I'm doing now, and link it here.

  16. moved this from 📝 To-Do to ✅ Done in UXarray Developmenton Aug 19, 2026
  17. moved this from ✅ Done to No plans for now in UXarray Developmenton Aug 19, 2026
  18. removed
    improvement
    on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

scalability

Fields

Priority

None yet

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions