emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Recent changes to add-to-list


From: David Kastrup
Subject: Recent changes to add-to-list
Date: Sun, 29 Oct 2006 10:38:07 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

Hi,

recently add-to-list got an optional comparison function argument, and
Kim optimized a few general cases.

The main problem I have with this is that it incurs additional runtime
overhead even for the default case, and even though the comparison
function will be function almost always.  So I would think it more
reasonable to add separate functions, add-to-listq, add-to-listql and
add-to-list-whatever.  That is verbose on obarray, but does not impact
the general use case.

Alternatively, the optimization could be done at compile time using
macros.  But I don't know whether there might be cases where
add-to-list is required to be a function, and it seems like a bad idea
to change such a frequently used function to a macro, in particular
at the present point of time.

Then I have qualms about the implementation:

(defun add-to-list (list-var element &optional append compare-fn)
[...]
  (if (cond
       ((null compare-fn)
        (member element (symbol-value list-var)))
       ((eq compare-fn 'eq)
        (memq element (symbol-value list-var)))
       ((eq compare-fn 'eql)
        (memql element (symbol-value list-var)))
       (t
        (let (present)
          (dolist (elt (symbol-value list-var))
            (if (funcall compare-fn element elt)
                (setq present t)))
          present)))

Note that this walks through the entire list even if the first
comparison already establishes the presence of a list element.  That
is very bad, since that is likely the most common use case.

It would make much more sense to do something like

     (t
       (let ((lst (symbol-value list-var)))
         (while (and lst
                     (null (funcall compare-fn element (car lst))))
             (setq lst (cdr lst)))
          lst)))

instead.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum




reply via email to

[Prev in Thread] Current Thread [Next in Thread]