Skip to content

Conversation

@BabaSanfour
Copy link

This PR resolves two critical issues observed on macOS Apple Silicon (M1/M2/M3) when building with Numpy 2.x:

  1. SIGBUS (Bus Error) Crash:
    Issue: The Node structures in src/annoylib.h rely on a memory layout where children and v (vector data) are contiguous. On ARM64, strict alignment requirements caused SIGBUS crashes when accessing these potentially unaligned structures.
    Fix: Added __attribute__((__packed__)) to the Node structs in Angular, DotProduct, Hamming, and Minkowski definitions. This ensures the compiler generates safe code for unaligned memory access.

  2. Incorrect Neighbor Count (Logic Error):

Issue: Queries were returning only 1 neighbor even when k=10 was requested. This was traced to the -ffast-math compiler flag, which enables non-IEEE 754 compliant optimizations. These optimizations likely interfered with Infinity or NaN handling in the priority queue comparisons or distance calculations.
Fix: Removed -ffast-math from the default compiler arguments in setup.py.

This solves the error in #682 #679 and would help some packages YingfanWang/PaCMAP#94

- Fix SIGBUS on macOS ARM64 by packing Node structs in `annoylib.h`. The unaligned memory layout of the nodes caused crashes on Apple Silicon.
- Remove `-ffast-math` from `setup.py`. This flag caused incorrect neighbor counts (returning 1 instead of k) due to unsafe floating-point optimizations affecting distance calculations and priority queue logic.
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