property list hack, profiling, compiler optimization, looking at code
Subject: property list hack, profiling, compiler optimization, looking at code
From: Richard Fateman
Date: Sat, 11 Aug 2012 17:14:02 -0700
On 8/11/2012 2:38 PM, Stavros Macrakis wrote:
> On Sat, Aug 11, 2012 at 2:11 PM, Richard Fateman
> <fateman at eecs.berkeley.edu <mailto:fateman at eecs.berkeley.edu>> wrote:
>
> An additional observation regarding speed and zerop1, which is
> defined this way:
>
> (defun zerop1 (x)
> (or (and (integerp x) (= 0 x))
> (and (floatp x) (= 0.0 x))
> (and ($bfloatp x) (= 0 (second x)))))
>
>
> This is a strange definition, as for a non-zero integer, it still
> checks floatp and $bfloatp.
oops!
> More straightforward would be:
>
> (defun zerop1 (x)
> (declare (inline $bfloatp)) ; worthwhile for such a common
> function (requires declaim/inline etc. @ $bfloatp)
> (cond ((fixnump x) (= 0 x)) ; a bignum can't be 0
> ((floatp x) (= 0.0 x))
> (($bfloatp x) (= 0 (second x)))
> (t nil)))
>
maybe we could use typecase
(defun zerop7(x) (declare (optimize (speed 3)(safety 0)))
(typecase x (number (zerop x))
(list (and (eq (caar x) 'bigfloat) (= (second
x) 0)))))
or even
(defun zerop10(x) (declare (optimize (speed 3)(safety 0)))
(if (numberp x)(zerop x)
(and(listp x)(if (eq (caar x) 'bigfloat)(eq (second x) 0))
;; this one is half the length (in assembler , in allegro) of the
original, and probably faster.
;; note that (= 0 0.0) is true, so there is no need to discriminate
between floats and fixnum and integers.
;; also if x is a fixnum I think that (eq x 0) will work faster than (=
x 0).
> Of course, the straightforward definition isn't necessarily the
> fastest, and may depend on the particular compiler and processor.
yes
>
> Though I find micro-optimizing like this peculiarly entertaining,
the hacker mentality...
> I'm pretty sure this will make no noticeable difference to the user
> experience,
agreed, though it may make things 1% faster :)
> certainly less difference than improving functionality in any one of a
> number of ways.
>
> -s
>
RJF