Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you incur a data-copying performance hit when passing arguments to Perl subroutines?

Tags:

perl

I have been working on several Perl scripts that process large fixed-width data files, extracting small substrings out of each data record. I had imagined that delegating the extracting of substrings to method calls would be costly because of the overhead of copying the data record into the @_ array. So I ran the following to compare (a) direct call to substr(), (b) method call passing the data record as a string, and (c) method call passing the data record by reference.

use strict;
use warnings;
use Benchmark qw(timethese);

my $RECORD = '0' x 50000;

my $direct = sub { my $v = substr( $RECORD, $_, 1) for 0..999 };
my $byVal  = sub { my $v = ByVal ( $RECORD, $_)    for 0..999 };
my $byRef  = sub { my $v = ByRef (\$RECORD, $_)    for 0..999 };

sub ByVal { return substr(   $_[0], $_[1], 1) }
sub ByRef { return substr(${$_[0]}, $_[1], 1) }

timethese( 10000, {
    direct    => $direct,
    byVal     => $byVal,
    byRef     => $byRef,
} );

my $byVal2loc  = sub { my $v = ByVal2loc( $RECORD, $_) for 0..999 };
my $byRef2loc  = sub { my $v = ByRef2loc(\$RECORD, $_) for 0..999 };

sub ByVal2loc { my $arg = shift; return substr(  $arg, $_[0], 1) }
sub ByRef2loc { my $arg = shift; return substr( $$arg, $_[0], 1) }

timethese( $ARGV[0], {
    byVal2loc => $byVal2loc,
    byRef2loc => $byRef2loc,
} );

# Produces this output:
Benchmark: timing 10000 iterations of byRef, byVal, direct...
     byRef: 19 wallclock secs...
     byVal: 15 wallclock secs...
    direct:  4 wallclock secs...

Benchmark: timing 10000 iterations of byRef2loc, byVal2loc...
 byRef2loc: 21 wallclock secs...
 byVal2loc: 119 wallclock secs...

As expected, the direct method was the fastest. However, I was surprised to find no penalty related to the "copying of data" that I had been imagining. Even when I increased the width of the record to outlandish proportions (for example, a billion characters), the by-value and by-reference benchmarks were basically the same.

It seems that when passing arguments to methods, Perl does not copy data. I guess this makes sense upon further reflection about the aliasing power of @_. The arguments are passed by reference, not by value.

However, it is a limited form of by-reference passing, because the references in @_ cannot be assigned directly to a local variable within the subroutine. Such assignments do result in data copying, as illustrated by the second set of benchmarks.

Am I understanding this correctly?

like image 905
FMc Avatar asked Jun 15 '09 21:06

FMc


2 Answers

Yes, assignments copy; just passing arguments do not. You can alias lexicals to elements in @_ using Lexical::Alias, however. This modified benchmark shows doing that a third as fast as using a reference, but consistently so regardless of the length of $RECORD:

use strict;
use warnings;
use Benchmark qw(timethese);
use Lexical::Alias;

my $RECORD = '0' x 5000000;

my $byVal2loc  = sub { my $v = ByVal2loc( $RECORD, $_) for 0..999 };
my $byRef2loc  = sub { my $v = ByRef2loc(\$RECORD, $_) for 0..999 };
my $byAlias2loc = sub { my $v = ByAlias2loc( $RECORD, $_ ) for 0..999 };

sub ByVal2loc { my $arg = shift; return substr(  $arg, $_[0], 1) }
sub ByRef2loc { my $arg = shift; return substr( $$arg, $_[0], 1) }
sub ByAlias2loc { my $arg; alias($_[0], $arg); return substr( $arg, $_[0], 1  ) }

timethese( $ARGV[0], {
    byVal2loc => $byVal2loc,
    byRef2loc => $byRef2loc,
    byAlias2loc => $byAlias2loc,
} );

# output:
Benchmark: running byAlias2loc, byRef2loc, byVal2loc for at least 3 CPU seconds...
byAlias2loc:  3 wallclock secs ( 3.16 usr +  0.00 sys =  3.16 CPU) @ 430.70/s (n=1361)
 byRef2loc:  4 wallclock secs ( 3.24 usr +  0.00 sys =  3.24 CPU) @ 1329.63/s (n=4308)
 byVal2loc:  5 wallclock secs ( 4.95 usr +  0.01 sys =  4.96 CPU) @  0.40/s (n=2)
            (warning: too few iterations for a reliable count)

(Directly using alias_r instead of the alias helper function is marginally faster.)

like image 51
ysth Avatar answered Nov 02 '22 18:11

ysth


IIRC, in a Perl 'sub', the @_ array is already a set of aliases (references) to the variables. If you modify $_[0], you affect the variable in the calling function.

#!/bin/perl -w
use strict;

sub x
{
    print "x = $_[0]\n";
    $_[0] = "pinkerton";
    print "x = $_[0]\n";
}

my $y = "abc";

print "y = $y\n";
x($y);
print "y = $y\n";

The output is:

y = abc
x = abc
x = pinkerton
y = pinkerton
like image 24
Jonathan Leffler Avatar answered Nov 02 '22 17:11

Jonathan Leffler