[Openmcl-devel] Type-of and positive/negative integers

Stas Boukarev stassats at gmail.com
Thu Oct 24 07:28:03 PDT 2019


On Thu, Oct 24, 2019 at 4:07 PM Steven Nunez <steve_nunez at yahoo.com> wrote:
>
> I'm doing this for performance. Here's the whole function; the coefficients typically have between 5-15 elements.
>
> (declaim (inline evaluate-polynomial))
> (defun %evaluate-polynomial (coefficients x)
>   "Return the sum of polynomials, weighted by the list of COEFFICIENTS, at X.
> X and contents of COEFFICIENTS must be of the same type.
> COEFFICIENTS are in descending order from the highest degree down to the constant term."
>   (declare (optimize(speed 3)(safety 0))
>        #+sbcl (sb-ext:muffle-conditions sb-ext:compiler-note)) ; Function declared inline, but SBCL whines
>        ;; See https://groups.google.com/forum/#!msg/comp.lang.lisp/AsKvR0emFtU/ILVjmLTlJ50J
>   (assert (and (plusp (length coefficients))
>              (every (lambda (elt)
>               (typep elt (class-of x)))
>                 coefficients))
>       (coefficients x)
>       "Coefficients and X must be of the same type.")
>   (etypecase x
>     (double-float
>      (let((sum (car coefficients)))
>        (declare (double-float sum x))
>        (loop for i double-float in (cdr coefficients)
>       do (setf sum (+ i (* x sum))))
>        sum))
>     (single-float
>      (let((sum (car coefficients)))
>        (declare (single-float sum x))
>        (loop for i single-float in (cdr coefficients)
>       do (setf sum (+ i (* x sum))))
>        sum))
>     (fixnum
>      (let((sum (car coefficients)))
>        (declare (fixnum sum x))
>        (loop for i fixnum in (cdr coefficients)
>       do (setf sum (+ i (* x sum))))
>        sum))))
>
> I haven't needed to optimize to any great degree before, but here we're at the bottom levels and this gets called a great many times. I'm just a little surprised there is such a high cost in typep. As I understand your reply, even doing this over a small sequence is going to kill performance (or rather make the declarations pointless given the cost/benefit of the assert).
On SBCL (and probably CCL too, with high enough SAFETY), your
declarations in the loops for the elements will perform type checks,
albeit without providing as nice an error message.



More information about the Openmcl-devel mailing list