| Store | Cart

Re: Optimizing length() in boolean context? (was: Re: Strings in boolean context - a mistake?)

From: Zefram <zef...@fysh.org>
Fri, 28 Aug 2015 11:28:03 +0100
Lasse Makholm wrote:
>I'm curious as to why you think it cargo-culting...?

It is used in the manner of an idiom, but is rarely a sensible choice.
It's intrinsically somewhat wasteful, because it's extracting
rich information that is being immediately thrown away.  (As Reini
pointed out, the waste is mild if your strings are always downgraded.)
Where it's used, usually the operand is known to be a defined string,
and the condition being computed is really $x ne "".  Writing it in this
more direct form is clearer and avoids the unbounded expense on long
upgraded strings.  On other occasions the operand is either undef or a
non-empty string, and the condition being computed is really defined($x),
which is again clearer and more efficient when written directly.

length($x) in truth-value context is only useful (by way of minimising the
number of operators used) where the operand may be undef, empty string,
or non-empty string, *and* undef and empty string are to be treated the
same, differently from non-empty string.  This is a pretty rare need.
The disadvantage of unbounded computation time on upgraded strings
means that even then one should think carefully before favouring it over
($x//"") ne "" and the like, which is in any case clearer.

Most people using length($x) in truth-value context are not using it
because of this rare need and a considered appreciation of the pros,
cons, and alternatives.  Rather, they've been given this knowledge packet
that this is the way to overcome problems with strings as truth values,
and so they blindly apply it every time.  It's not popular on merit,
it's popular by cargo cult.

>Interesting! I did not know about Devel::GoFaster... Is there anything>written anywhere (apart from the code) about what it actually does and>impact it has?

No.  The only documentation of the optimisations is the commentary in
the code.  As for impact, you should run your own performance tests, on
your real code and data.  Don't expect anything spectacular from D:GF:
it'll be fractions of a percentage point.

-zefram

Recent Messages in this Thread
Ed Avis Aug 27, 2015 07:13 am
Victor Efimov Aug 27, 2015 07:48 am
Aristotle Pagaltzis Aug 27, 2015 10:43 am
Zefram Aug 27, 2015 10:50 am
Lasse Makholm Aug 27, 2015 10:56 am
Todd Rinaldo Aug 27, 2015 04:26 pm
Rocco Caputo Aug 27, 2015 04:39 pm
Zefram Aug 27, 2015 05:54 pm
Ed Avis Aug 27, 2015 06:05 pm
Dagfinn Ilmari Mannsåker Aug 27, 2015 06:15 pm
Zefram Aug 27, 2015 06:50 pm
Ed Avis Aug 27, 2015 07:19 pm
Rocco Caputo Aug 28, 2015 02:20 pm
Dagfinn Ilmari Mannsåker Aug 28, 2015 02:35 pm
Dagfinn Ilmari Mannsåker Aug 28, 2015 02:55 pm
Lasse Makholm Aug 27, 2015 09:02 pm
Aristotle Pagaltzis Aug 28, 2015 10:45 pm
Lasse Makholm Aug 27, 2015 09:42 pm
Zefram Aug 28, 2015 10:28 am
Aristotle Pagaltzis Aug 28, 2015 11:54 pm
Lasse Makholm Aug 31, 2015 11:10 pm
Reini Urban Aug 28, 2015 08:32 am
Eric Brine Aug 29, 2015 03:49 pm
Lasse Makholm Aug 31, 2015 10:36 pm
Yitzchak Scott-Thoennes Aug 31, 2015 10:48 pm
Zefram Sep 01, 2015 08:50 am
Eric Brine Sep 02, 2015 05:22 pm
Ed Avis Aug 27, 2015 02:35 pm
H.Merijn Brand Aug 27, 2015 02:50 pm
Ed Avis Aug 27, 2015 02:54 pm
Zefram Aug 27, 2015 03:10 pm
Ed Avis Aug 27, 2015 03:35 pm
Zefram Aug 27, 2015 03:53 pm
Ed Avis Aug 27, 2015 04:11 pm
Abigail Aug 27, 2015 04:17 pm
Ed Avis Aug 27, 2015 04:26 pm
Ed Avis Aug 27, 2015 04:15 pm
Kent Fredric Aug 27, 2015 03:26 pm
Abigail Aug 27, 2015 04:09 pm
Ed Avis Aug 27, 2015 04:21 pm
Abigail Aug 27, 2015 04:38 pm
Messages in this thread