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: Sun, 27 Dec 2020 10:52:42 -0800 (PST)

> > 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.
> 
> length is different than map and dolist.

Everything that's not the same is different.

> length is about a property of a list, namely about number of items.
> map and dolist are about doing something with items of list.

That's a quibble.  Length is about doing something
with the list elements: counting them.  It's no more
about a property of a list than is a function that
checks all the element types or, like `member',
finds an element that satisfies some condition.

> Using length in a predicate is yet completely different and most likely
> bad because to answer the predicate, traversing the whole list is wasted
> time and energy.

(lambda (xs) (= (length xs) 25))  ; need to count elts

> > There are no specific functions that can be
> > used to avoid the trap.
> 
> There are not, but there could be.

No, not completely.  The gotcha is much more general
than the cases that could be "fixed" using the
proposed predicates.

> > I see no help offered by `length<' & compagnie.
> 
> The idea is that the intent is expressed in a way that can _guarantee_
> that useless length computation is avoided.

There are plenty of other possibilities for uselessly
invoking `length'.  Probably the most typical one is
neglecting to find the length only once and save it
in a local variable.

How much existing code (probably/hopefully mostly
outside the core code) does that, instead of this?

(let ((len  (length xs))) ... len ...len ... len ...)

A quick grep won't find places where code unwisely instead
does ... (length xs) ... (length xs) ... (length xs).

My guess is that those oblivious to the problem/gotcha
do that much more often than they do a single `length'
in a predicate that doesn't logically need to traverse
the whole list.

The point is that it's possible to misuse `length'
(and `dolist' and `member' and ...).  And no creation
of `length<' etc. predicates helps.  It fact, it can
hurt, as I explained previously.
 
> > IOW, this stuff is best considered case by case.

> > 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.
> 
> But how do you know if it has or has not performance impact unless you
> investigate each case?

See above - this is best done case by case.  As with
all attempts to improve performance.  Do it when it's
appropriate/needed.  If you don't know whether it's
needed then don't do it.  If it's really needed you'll
know or you'll find out.

Premature or otherwise misguided optimization essentially
works against Occam's razor.  Relevant/necessary
optimization is just the opposite: it applies Occam's
razor. 

> > I wouldn't be in favor of systematically avoiding use
> > of `length', as if all uses are poisonous.
> 
> In fact, it kind of is, especially when used in a predicate.

No, it kind of isn't.  Find the _actual_ places where
the core Emacs code is _really_ problematic.  See how
many there are, compared to that shotgun-blast `grep'.
Fix occurrences where the performance matters, i.e.,
those that are really problematic.  End of story.

`length<' & compagnie: YAGNI.



reply via email to

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