emacs-devel
[Top][All Lists]
Advanced

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

RE: lengths and stuff


From: Drew Adams
Subject: RE: lengths and stuff
Date: Sat, 26 Dec 2020 21:23:10 -0800 (PST)


> -----Original Message-----
> From: Daniel Brooks <db48x@db48x.net>
> Sent: Saturday, December 26, 2020 8:59 PM
> To: Drew Adams <drew.adams@oracle.com>
> Cc: Eli Zaretskii <eliz@gnu.org>; Stefan Monnier <monnier@iro.umontreal.ca>;
> larsi@gnus.org; emacs-devel@gnu.org
> Subject: lengths and stuff
> 
> Drew Adams <drew.adams@oracle.com> writes:
> 
> > With that "solution" you've only added a new problem: you
> > now need to advertise the new functions and advise users
> > to use them, not `length', in such a use case.  Which
> > means you still have the need to identify the relevant use
> > cases, which means teach them about the `length' gotcha
> > (or hope they follow advice without understanding).  IOW,
> > now you have 2 problems...
> 
> Users certainly do need to know the performance cost of the functions
> that they use. But they also need alternatives. If (> (length list) n)
> is a trap for new players, then document it as so _and_ mention that
> there is a specific function that can be used to avoid the trap. But
> it's also worth mentioning that maybe the code should use a vector
> instead of a list, if the length is so important.

I spoke to that (e.g. doc guidance) a bit already:

https://lists.gnu.org/archive/html/emacs-devel/2020-12/msg01757.html

And as I said, `length' is not so special in this regard.
`map', `dolist', and others also provide the same rope to
hang yourself with.

Learning to `throw' out of a list traversal (or whatever)
when you can tell it's time to quit is a _general_
solution/idiom - good to know about even if it's not
always the most appropriate.  That should be taught in
the manuals (in this general don't-continue-needlessly
context), even if many prefer things like `cl-loop'.

There are no specific functions that can be used to avoid
the trap.  Not in general.  Code that DTRT in any given
context can be short, sweet, and clear.  I see no help
offered by `length<' & compagnie.

And if the list in question is short, and the context is
not performance-critical (e.g. tight loop that's important),
then there may be no reason to bother about not traversing
it entirely.  IOW, this stuff is best considered case by
case.  Lisp already has all that's needed to create clear
code that does what one wants AND conveys to human readers
what's done and why. 

> Also, as was pointed out, this very thing happens 735 times in the Emacs
> lisp codebase. Probably most of those have no great performance impact,
> but it's a good observation.

If a given case has no performance impact, then it's
maybe better NOT to provide code that might give the
impression that we're trying to avoid a particular
performance penalty.  Of course in middle cases, which
might not be obvious to a human reader, we may need to
make things clear explicitly.

I wouldn't be in favor of systematically avoiding use
of `length', as if all uses are poisonous.  That would
almost be akin to wrapping all uses of `dolist' in a
`catch'.  It sends human readers the wrong message.
When it's important to avoid traversing more than
necessary, make that clear.  Avoiding it _always_ makes
it less clear when it's actually needed, not more.

(Just one opinion.)



reply via email to

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