Skip to content

#1789 2c: sqrt optimization #1917

Closed as not planned
Closed as not planned

Description

@cmdupuis3

Inside @njit, math.sqrt/np.sqrt do lower to a libm sqrt call, but LLVM turns that into the native fsqrt whenever it can prove the argument non-negative (e.g. a sum of squares). Checking the optimized LLVM IR on main shows the four area.py sites, the gradient.py norms, and error_radius (inlined into _unique_points) already compile to fsqrt. Rewriting them as ** 0.5 adds a fabs and a -inf check and made the area Jacobian ~5% slower, so they are unchanged. The sites that really did call libm are acc_sqrt_re, compute_arc_length, haversine_distance, gradient's _compute_arc_length, neighborhood std and _numba_sqrt3. These now use ** 0.5, and _newton_quadrilateral compares norm2 < 1e-30 instead of taking a sqrt. The _numba_norm3 comment now gives the actual cause. The _unique_points denominator hoist was skipped: it costs an allocation and is slower for the 2–8 points callers pass. Results are bit-identical to main across 16 checked outputs, and new tests pin acc_sqrt_re's rounding, its rejection of degenerate inputs, and its compiling to llvm.sqrt without a libm call. Performance is unchanged within noise on arm64 macOS, including the batched SameBodyConstLat/SameBodyGcaGca asv benchmarks, zonal_mean and gradient; x86 is untested. Each changed sqrt is one operation among dozens in its kernel, so 2c should be dropped from the performance list.

Activity

  1. moved this from 📚 Backlog to 🏗 In progress in UXarray Developmenton Nov 20, 2026
  2. cmdupuis3 commented on Nov 20, 2026

    @cmdupuis3
    CollaboratorAuthor

    Optimization benefits seem to be minor, closing as not planned

  3. moved this from 🏗 In progress to ✅ Done in UXarray Developmenton Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions