Mailing List Archive

When was %hash in boolean context optimized?
I believe C<< if (%hash) >> was optimized to avoid creating a string at
some point. Does anyone know when? (It would have been before 5.26, since
it stopped returning a string completely at that point.)
Re: When was %hash in boolean context optimized? [ In reply to ]
On Mon, 3 Aug 2020 11:05:54 -0400, Eric Brine <ikegami@adaelis.com>
wrote:

> I believe C<< if (%hash) >> was optimized to avoid creating a string
> at some point. Does anyone know when? (It would have been before
> 5.26, since it stopped returning a string completely at that point.)

Do you have test code to show exactly what you mean?



--
H.Merijn Brand http://tux.nl Perl Monger http://amsterdam.pm.org/
using perl5.00307 .. 5.31 porting perl5 on HP-UX, AIX, and Linux
https://useplaintext.email https://tux.nl http://www.test-smoke.org
http://qa.perl.org http://www.goldmark.org/jeff/stupid-disclaimers/
Re: When was %hash in boolean context optimized? [ In reply to ]
On Mon, Aug 3, 2020 at 11:21 AM H.Merijn Brand <perl5@tux.freedom.nl> wrote:

> On Mon, 3 Aug 2020 11:05:54 -0400, Eric Brine <ikegami@adaelis.com>
> wrote:
>
> > I believe C<< if (%hash) >> was optimized to avoid creating a string
> > at some point. Does anyone know when? (It would have been before
> > 5.26, since it stopped returning a string completely at that point.)
>
> Do you have test code to show exactly what you mean?
>

C<< scalar(%hash) >> used to produce a string like C<< 3/8 >> if it had any
elements. C<< if (%h) >> was optimized to avoid the string creation.

It looks like it might have been as far back as 5.12, though something
changed in 5.18.

$ 5.10t/bin/perl -MO=Concise,-exec -e'if (%h) {}'
...
4 <1> rv2hv[t2] sK/1
5 <|> and(other->6) vK/1
...

$ 5.12t/bin/perl -MO=Concise,-exec -e'if (%h) {}'
...
4 <1> rv2hv[t2] sKRM/1
5 <1> boolkeys sK/1
6 <|> and(other->7) vK/1
...

$ 5.16t/bin/perl -MO=Concise,-exec -e'if (%h) {}'
...
4 <1> rv2hv[t2] sKRM/1
5 <1> boolkeys sK/1
6 <|> and(other->7) vK/1
...

$ 5.18t/bin/perl -MO=Concise,-exec -e'if (%h) {}'
...
4 <1> rv2hv[t2] sK/BOOL,1
5 <|> and(other->6) vK/1
...

I hunted through all the perldeltas before email earlier, but it seems I
missed the following bit from perl5120delta:

C<< if (%foo) >> has been optimized to be faster than C<< if (keys %foo) >>.
Re: When was %hash in boolean context optimized? [ In reply to ]
On Mon, 3 Aug 2020 12:11:26 -0400, Eric Brine <ikegami@adaelis.com>
wrote:

> On Mon, Aug 3, 2020 at 11:21 AM H.Merijn Brand <perl5@tux.freedom.nl>
> wrote:
>
> > On Mon, 3 Aug 2020 11:05:54 -0400, Eric Brine <ikegami@adaelis.com>
> > wrote:
> >
> > > I believe C<< if (%hash) >> was optimized to avoid creating a
> > > string at some point. Does anyone know when? (It would have been
> > > before 5.26, since it stopped returning a string completely at
> > > that point.)
> >
> > Do you have test code to show exactly what you mean?
> >
>
> C<< scalar(%hash) >> used to produce a string like C<< 3/8 >> if it
> had any elements. C<< if (%h) >> was optimized to avoid the string
> creation.
>
> It looks like it might have been as far back as 5.12, though something
> changed in 5.18.
>
> $ 5.10t/bin/perl -MO=Concise,-exec -e'if (%h) {}'
> ...
> 4 <1> rv2hv[t2] sK/1
> 5 <|> and(other->6) vK/1
> ...
>
> $ 5.12t/bin/perl -MO=Concise,-exec -e'if (%h) {}'
> ...
> 4 <1> rv2hv[t2] sKRM/1
> 5 <1> boolkeys sK/1
> 6 <|> and(other->7) vK/1
> ...
>
> $ 5.16t/bin/perl -MO=Concise,-exec -e'if (%h) {}'
> ...
> 4 <1> rv2hv[t2] sKRM/1
> 5 <1> boolkeys sK/1
> 6 <|> and(other->7) vK/1
> ...
>
> $ 5.18t/bin/perl -MO=Concise,-exec -e'if (%h) {}'
> ...
> 4 <1> rv2hv[t2] sK/BOOL,1
> 5 <|> and(other->6) vK/1
> ...
>
> I hunted through all the perldeltas before email earlier, but it
> seems I missed the following bit from perl5120delta:
>
> C<< if (%foo) >> has been optimized to be faster than C<< if (keys
> %foo) >>.

$ perl-all -e 'my%hash=(1,2);print scalar%hash,"\n"'

Running perl-all -e my%hash=(1,2);print scalar%hash,"\n"
On 442 versions of perl
ranging from perl5.6.0 to tperl5.33.0
=== base/perl5.6.0 5.006 x86_64-linux
1/8
=== base/perl5.6.1 5.006001 x86_64-linux-perlio
1/8
=== base/tperl5.6.1 5.006001 x86_64-linux-thread-multi-ld-perlio
1/8
:
:
=== base/tperl5.25.0 5.025000 x86_64-linux-thread-multi-ld
1/8
=== base/perl5.25.1 5.025001 x86_64-linux
1/8
=== base/tperl5.25.1 5.025001 x86_64-linux-thread-multi-ld
1/8
=== base/perl5.25.2 5.025002 x86_64-linux
1/8
=== base/tperl5.25.2 5.025002 x86_64-linux-thread-multi-ld
1/8
=== base/perl5.25.3 5.025003 x86_64-linux
1
=== base/tperl5.25.3 5.025003 x86_64-linux-thread-multi-ld
1
=== base/perl5.25.4 5.025004 x86_64-linux
1
=== base/tperl5.25.4 5.025004 x86_64-linux-thread-multi-ld
1
=== base/perl5.25.5 5.025005 x86_64-linux
1
=== base/tperl5.25.5 5.025005 x86_64-linux-thread-multi-ld
1
=== base/perl5.25.6 5.025006 x86_64-linux
1


--
H.Merijn Brand http://tux.nl Perl Monger http://amsterdam.pm.org/
using perl5.00307 .. 5.31 porting perl5 on HP-UX, AIX, and Linux
https://useplaintext.email https://tux.nl http://www.test-smoke.org
http://qa.perl.org http://www.goldmark.org/jeff/stupid-disclaimers/
Re: When was %hash in boolean context optimized? [ In reply to ]
On Mon, Aug 03, 2020 at 12:11:26PM -0400, Eric Brine wrote:
> I hunted through all the perldeltas before email earlier, but it seems I
> missed the following bit from perl5120delta:
>
> C<< if (%foo) >> has been optimized to be faster than C<< if (keys %foo) >>.

%h in boolean context was optimised in 5.12.0 and further tweaked in 5.18.0.

%h in scalar context was changed in 5.26 to return a number rather than a
string.

'keys %h' in boolean context was optimised in 5.28.0.

--
Wesley Crusher gets beaten up by his classmates for being a smarmy git,
and consequently has a go at making some friends of his own age for a
change.
-- Things That Never Happen in "Star Trek" #18