Skip to content

Commit f486cbf

Browse files
TomasRikerrtoy
authored andcommitted
NORMALIZED-MODULUS: access specvar MODULUS only once
Accessing special variables is expensive on some Lisps, e.g. SBCL. Reduce the number of accesses to MODULUS by using a temporary variable. CMOD (and NORMALIZED-MODULUS) are heavily used by the CRE package whenever coefficients are added, multiplied etc. This change makes CMOD about 7% faster when MODULUS is set to non-NIL on SBCL (no difference when MODULUS is NIL).
1 parent b63cd4a commit f486cbf

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

src/rat3a.lisp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,10 @@
188188
(defun normalized-modulus (n)
189189
"Normalizes the number N with respect to MODULUS,
190190
returning a number in (-MODULUS/2, MODULUS/2]."
191-
(declare (type number n))
192-
(let ((rem (mod n modulus)))
193-
(if (<= (* 2 rem) modulus)
191+
(let* ((m modulus) (rem (mod n m)))
192+
(if (<= (* 2 rem) m)
194193
rem
195-
(- rem modulus))))
194+
(- rem m))))
196195

197196
;; CMOD
198197
;;

0 commit comments

Comments
 (0)