You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Vectorize _on_minor_arc_xyz for improved large-data processing performance #1623
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.
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.
@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.
@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.
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.
@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.
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.
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?
Activity
cmdupuis3 commentedon Jul 27, 2026
I'll add this to my portfolio of optimization work.
rajeeja commentedon Jul 27, 2026
cool, thanks! while you are at it scan for more stuff like this.
hongyuchen1030 commentedon Jul 27, 2026
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
cmdupuis3 commentedon Jul 27, 2026
@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.
hongyuchen1030 commentedon Jul 28, 2026
That make sense, different compiler has different behavior when it comes to boolean.
intshould probably have a more steady behavior.Sevans711 commentedon Aug 19, 2026
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_xyzare 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.
cmdupuis3 commentedon Aug 19, 2026
@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.cmdupuis3 commentedon Aug 19, 2026
After investigating this, my conclusion is that
_on_minor_arc_xyzis 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_xyzwhere it's called.erogluorhan commentedon Aug 19, 2026
That said, should this issue be closed as unplanned?
cmdupuis3 commentedon Aug 19, 2026
I think that makes sense, but I'll add a new issue for what I'm doing now, and link it here.