unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Recent changes to add-to-list
@ 2006-10-29  9:38 David Kastrup
  2006-10-30  1:05 ` Stefan Monnier
  2006-10-30 13:33 ` Richard Stallman
  0 siblings, 2 replies; 8+ messages in thread
From: David Kastrup @ 2006-10-29  9:38 UTC (permalink / raw)



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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-11-02  4:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-10-29  9:38 Recent changes to add-to-list David Kastrup
2006-10-30  1:05 ` Stefan Monnier
2006-10-30 13:33 ` Richard Stallman
2006-10-30 22:48   ` David Kastrup
2006-10-30 23:05     ` Nick Roberts
2006-11-01  2:13       ` Richard Stallman
2006-11-01 16:39         ` Stefan Monnier
2006-11-02  4:43           ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).