Skip to content

perf: minor optimizations — clone reduction + det_sign_exact short-circuit #66

Description

@acgetchell

Summary

Small targeted optimizations to reduce unnecessary clones and add a short-circuit path in det_sign_exact.

Proposed Changes

Clone reduction in bareiss_det

The inner loop already uses references correctly:

a[i][j] = (&a[k][k] * &a[i][j] - &a[i][k] * &a[k][j]) / &prev_pivot;

But the pivot assignment clones unconditionally:

prev_pivot = a[k][k].clone();

This clone is necessary (mutation boundary), but the intermediate pivot reference in the elimination loop can be hoisted:

let pivot_ref = &a[k][k];
// use pivot_ref in inner loop instead of re-borrowing a[k][k]

Clone reduction in gauss_solve

Same pattern — pivot = aug[k][k].clone() on line 179 can be deferred or replaced with a reference where the borrow checker allows.

Short-circuit in det_sign_exact

Currently both det_direct() and det_errbound() are always computed together:

if let (Some(det_f64), Some(err)) = (self.det_direct(), self.det_errbound()) {

Micro-optimization: compute det_direct() first, and skip det_errbound() if the magnitude is obviously large relative to machine epsilon. The permanent computation in det_errbound() is non-trivial for D=3,4.

if let Some(det_f64) = self.det_direct() {
    if det_f64.is_finite() {
        if let Some(err) = self.det_errbound() {
            if det_f64 > err { return Ok(1); }
            if det_f64 < -err { return Ok(-1); }
        }
    }
}

This is a minor restructure that avoids the errbound computation when det_f64 is non-finite (overflow case).

Benefits

  • Marginal speedup from reduced cloning (most visible for large BigRational values)
  • Cleaner separation of the two-stage filter in det_sign_exact
  • Low risk — minimal code changes

Implementation Notes

Activity

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

    performancePerformance related issuesrustPull requests that update rust code

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions