Dear Monks and Nuns,
lately I came across an issue with dereferencing array refs. It looks like there is some hidden "lazy deref" when an array ref is used in a foreach loop, compared to the usage as a sub argument.
Consider these two subs that do nothing but die.
The main difference is the array dereference as an argument to foreach or map.
Benchmarking is amazing:use experimental 'signatures'; sub map_die ($ar) { eval {map {die} @$ar}; } sub for_die ($ar) { eval { for (@$ar) { die; } } }
use Benchmark 'cmpthese'; my @arr = ((0) x 1e6); cmpthese(0, { map => sub {map_die(\@arr)}, for => sub {for_die(\@arr)}, }); __DATA__ Rate map for map 1257/s -- -100% for 1664823/s 132352% --
Then I remembered the "lazy generators" from List::Gen and gave it a try. There is some progress, but it cannot come up to foreach.
use List::Gen 'array'; sub gen_die ($ar) { eval { &array($ar)->map(sub {die}); } } cmpthese(0, { map => sub {map_die(\@arr)}, for => sub {for_die(\@arr)}, gen => sub {gen_die(\@arr)}, }); __DATA__ Rate map gen for map 1316/s -- -93% -100% gen 18831/s 1330% -- -99% for 1662271/s 126174% 8727% --
Wouldn't it be nice to have some kind of "explicit lazy dereferencing" in Perl?
Update May 10, 2024: Added the signatures feature as suggested by Danny in Re: The Virtue of Laziness.
Greetings,
🐻
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: The Virtue of Laziness
by hv (Prior) on May 05, 2024 at 13:48 UTC | |
by jo37 (Curate) on May 05, 2024 at 14:55 UTC | |
by LanX (Saint) on May 05, 2024 at 14:33 UTC | |
by LanX (Saint) on May 05, 2024 at 15:07 UTC | |
by jo37 (Curate) on May 12, 2024 at 21:51 UTC | |
Re: The Virtue of Laziness
by LanX (Saint) on May 04, 2024 at 22:28 UTC | |
by jo37 (Curate) on May 05, 2024 at 07:14 UTC | |
by LanX (Saint) on May 05, 2024 at 10:14 UTC | |
by jo37 (Curate) on May 12, 2024 at 21:46 UTC | |
Re: The Virtue of Laziness
by Danny (Chaplain) on May 05, 2024 at 19:54 UTC | |
by jo37 (Curate) on May 06, 2024 at 08:33 UTC | |
Re: The Virtue of Laziness
by jo37 (Curate) on May 12, 2024 at 21:43 UTC | |
by hv (Prior) on May 12, 2024 at 23:49 UTC |