Perl 5 Internals - Part Two

Welcome back for another exciting episode on the inner workings of the perl interpreter! Last time we covered some of the basic optimizations perl performs on SVs, as well as the consequences of those optimizations. This time, we'll be going over some of the optimizations specific to strings and arrays. I know I said we'd be covering hashes too, but this article is already quite lengthy, and I have enough material on hashes to merit its own article, so look for that information in the upcoming third part of this series!

Strings

Since Perl was designed to perform operations on bodies on text, you can imagine that it has some clever optimizations for doing so. One set of optimizations is that trimming from either end of a string is cheap.

You can easily imagine how this might work from the end of the string; simply place a NUL character after the new end of the string, and update the length field. Many string implementations do this, and it should come as no surprise that perl does this as well. Similar to the "never downgrade" policy that I mentioned in the previous entry, a character buffer in perl is never shrunk, because perl figures you'll need it again, or at least that you'll be throwing away that value soon enough anyway.

Perl optimizes removing characters from the beginning of a string as well. It accomplishes this by setting the OOK flag on the SV (which means "offset ok"), updating the buffer pointer to point offset bytes past the start of the buffer, and setting the SV's OFFSET field the offset (in newer perls, this is placed in the string buffer itself). For example:

use Devel::Peek; my $s = 'foobar' ; Dump($s); $s =~ s/^foo// ; Dump($s);

Finally, when perl calls free() to deallocate the buffer, it uses the offset to calculate the pointer to pass to free() .

Appending to a string is also cheap, if the SV's buffer is large enough (ie. if LEN is big enough). Perl simply updates the CUR field and places a NUL character at the appropriate spot in the buffer:

my $s = 'foo' ; Dump($s); $s .= 'bar' ; Dump($s);

Of course, certain operations will end up reallocating the buffer, so be sure to benchmark and put Devel::Peek to good use!

Arrays

AVs benefit from a similar optimization:

my @array = qw /foo bar baz/; Dump(\@array); pop @array; Dump(\@array); shift @array; Dump(\@array);

( FILL is the last "valid" index of an array, equivalent to $#array )

Calling unshift on an array with the offset hack in place will make use of the free space:

unshift @array, 'quux' ; Dump(\@array);

If you're constantly pushing new elements onto an array, perl will realloc() new arrays as you go. This is fairly common in the implementation of dynamically sized arrays. If you know (or at least have a good idea) about how many elements your array will contain, you can use $#array as an lvalue to tell Perl to preallocate space for those elements:

$#array = $expected_size - 1 ;

This fills the array with undef values, though, so you'll have to calculate the insert index by hand if you do this.

my @names; $#names = 9 ; push @names, 'Rob' ; my $i = 0 ; $names[$i++] = 'Rob' ;

Of course, you could always set $#array to preallocate storage, and clear the array to be able to use push the normal way:

my @array; $#array = 99 ; @array = (); Dump(\@array); push @array, 'foo' ;

(I'm not sure what the MAGIC is here for in this case; that might merit some future research!)

Next Time

The next article will discuss how perl implements hashtables, along with some of the optimizations and security implications that come along with that implementation.

Please enable JavaScript to view the comments powered by Disqus.