From: Joerg-Cyril Hoehle
Subject: is (member # :test 'foo) -> :test #'foo optimization allowed?
Date: 
Message-ID: <u7k9kusk7.fsf@T-Systems.com>
Hi,

IIRC, either Tim Bradshaw or Kent Pitman recently said that an
implementation would be quite stupid not to do the above sort of
optimization for "sequence" functions like MEMBER, POSITION, SORT
etc.
The idea was to save one possible call to symbol-value for each
element and do that beforehand.

I'd like to clarify this issue, asking from the implementor's POV:

Is the following allowed:
(defun member/position/sort (x y &key (test #'identity) ...)
  (setq test (coerce test 'function))
  do/loop/recurse/iterate using (funcall test a b) ...)

Couldn't foo be a weird thing as follows:
(let ((latch 0))
  (defun reset (n)
    (setf latch n)
    (setf (symbol-function 'foo #'...)))
  (defun foo (x y)
    (when (zerop (decf latch))
      (setf (symbol-function 'foo) #'another-bar))
    ...))
which would make quite a difference!

(member/position/sort x y :test 'foo)
(member x y :test 'foo) vs. (member x y :test #'foo)

Thanks for your help,
	Joerg Hoehle
TSI ITC-Security Technologiezentrum

From: Paul F. Dietz
Subject: Re: is (member # :test 'foo) -> :test #'foo optimization allowed?
Date: 
Message-ID: <lZKdnTwVHuxmJTqjXTWcqA@dls.net>
Joerg-Cyril Hoehle wrote:
> Hi,
> 
> IIRC, either Tim Bradshaw or Kent Pitman recently said that an
> implementation would be quite stupid not to do the above sort of
> optimization for "sequence" functions like MEMBER, POSITION, SORT
> etc.
> The idea was to save one possible call to symbol-value for each
> element and do that beforehand.
> 
> I'd like to clarify this issue, asking from the implementor's POV:
> 
> Is the following allowed:
> (defun member/position/sort (x y &key (test #'identity) ...)
>   (setq test (coerce test 'function))
>   do/loop/recurse/iterate using (funcall test a b) ...)
> 
> Couldn't foo be a weird thing as follows:
> (let ((latch 0))
>   (defun reset (n)
>     (setf latch n)
>     (setf (symbol-function 'foo #'...)))
>   (defun foo (x y)
>     (when (zerop (decf latch))
>       (setf (symbol-function 'foo) #'another-bar))
>     ...))
> which would make quite a difference!
> 
> (member/position/sort x y :test 'foo)
> (member x y :test 'foo) vs. (member x y :test #'foo)


Section 3.2.2.3 might apply here.  If I'm reading it right, your
tricky case is undefined unless FOO is declared NOTINLINE.

	Paul
From: Paul F. Dietz
Subject: Re: is (member # :test 'foo) -> :test #'foo optimization allowed?
Date: 
Message-ID: <gK-cnWa80JlgXDqjXTWcoA@dls.net>
The section you want to look at is 1.4.1.5.

  Except as otherwise noted, in a situation where the denoted object might
  be used multiple times, it is implementation-dependent whether the object
  is coerced only once or whether the coercion occurs each time the object
  must be used.

	Paul