develooper Front page | perl.perl5.porters | Postings from April 2021

Why is my `any` keyword so slow?

Thread Next
From:
Paul "LeoNerd" Evans
Date:
April 23, 2021 21:11
Subject:
Why is my `any` keyword so slow?
Message ID:
20210423221116.64a3b3be@shy.leonerd.org.uk
While we're all sat twiddling our thumbs waiting for 5.34, I thought
I'd like to distract everyone with a bit of a performance puzzle.

Attached is a module tarball, not yet on CPAN, but which I had hoped
would be sometime soon. I was intending to make some native keyword
implementations of the List::Util functions - beginning with any and
all.

My plan was to create a keyword that acts exactly like List::Util::any
but is instead implemented in a pair of core ops much like grep is,
meaning it should run nicely fast, yes? Or at least, faster than the
XSUB that is List::Util::any.

Except, according to my benchmark it isn't. It is in fact significantly
slower than L:U:any. I manage to be faster than grep, which is
something. But it's very much slower than the List::Util version. Here
are numbers from a typical run:

$ perl -Mblib benchmark.pl 
                       Rate       CORE::grep List::Keywords/any  List::Util::any
CORE::grep          50238/s               --               -41%             -78%
List::Keywords/any  84611/s              68%                 --             -63%
List::Util::any    230967/s             360%               173%               --
Counts:
  grep: cmp=30952700 call=309527 (100.000 cmp/call)
  lka: cmp=26850327 call=526477 (51.000 cmp/call)
  lua: cmp=71686161 call=1405611 (51.000 cmp/call)


If anyone happens to find themselves with some spare time, I'd
appreciate any assistance or suggestions of something to look into
here, to see why my version is being so slow. Or is it the case that
List::Util's version just really is *that* much more efficient, given
as it uses dMULTICALL - at which point maybe we can adopt some of its
performance tricks into perl core's way of doing grep/etc... and make
them faster too?

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About