[Wellington-pm] comparison and assignment: insignificant optimisation

Olly Betts olly at survex.com
Mon Jun 16 04:09:08 PDT 2014


On Mon, Jun 16, 2014 at 10:42:37PM +1200, Ewen McNeill wrote:
> FWIW, in your example if the optimiser knows that length($foo) is
> constant for a constant input then it should be able to peephole
> optimise it with a temporary variable, so it's only calculated once.

A Perl scalar holding a string knows the string's length (it doesn't do
something like calling C's strlen() function each time - it can't as the
string might contain zero bytes) so length($foo) just loads a value from
a field in a C structure to get the length.  It may need to turn the
scalar into a string first, but if you call it more than once, only
the first call should need to do that.

> I guess my point here is that it may be premature optimisation
> unless you have a performance problem.

For this case, I suspect so, though Richard did say this was just
an example, and he was more concerned about similar cases but with a
more complex expression.

Cheers,
    Olly


More information about the Wellington-pm mailing list