The Day Perl Stood Still: Unveiling A Hidden Power Over C

Sometimes the unexpected happens and must be shared with the world … this one is such a case.

Recently, I’ve started experimenting with Perl for workflow management and high-level supervision of low level code for data science applications. A role I’d reserve for Perl in this context is that of lifecycle management of memory buffers, using the Perl application to “allocate” memory buffers and shuttle it between computing components written in C, Assembly, Fortran and the best hidden gem of the Perl world, the Perl Data Language. There at least 3 ways that Perl can be used to allocate memory buffers:

  1. Generate a list of bytes and use the pack function to convert them to a string.
  2. Use the repetition operator (x) to generate a string of length equal to the size of the buffer one wants minus one (strings are terminated via null bytes in Perl, thus the null byte compensates for the mine one).
  3. Access an external memory allocator library either through Inline or FFI::Platypus to allocate the buffer. When I took the three methods for a ride as detailed elsewhere, I found that allocating the buffer through a Perl string massively outperformed C’s malloc by over a 10-fold.

Not believing the massive performance gain, and thinking I am dealing with a bug in Inline::C, I recoded the allocation in pure C and obtained practically the same results as the Inline::C method.
After researching the issue further, I discovered that the malloc I grew to admire trades speed in memory allocation for generality, and there is a plethora of faster memory allocators out there. It seems that Perl is using one such allocator for its strings, and kicks C’s butt in this task of allocating buffers.

Repository of examples using Perl and Assembly together

Sometimes one needs an extra ounce of performance. Why not combine the high level semantics of Perl with the punch of assembly?

This repo includes various examples of how this can be done.

Caching & Memoization with state variables

Chapter 3 of Higher Order Perl describes various approaches to memoization of an expensive function: private cache and the Memoize module. The book was written in 2005 (Perl was at version 5.8 back then) , so it does not include another way for function caching that is now available : caching through state variables (introduced in Perl 5.10). The Fibonacci example considered in HOP also requires the ability to initialize state hash variables (available since Perl 5.28). The code below contrasts the implementation with a state…

Parallel Perl/C applications without tears using OpenMP: Controlling the OpenMP environment

Brett Estrade, did it again with yet another excellent talk at TPRC 2024 about the use of OpenMP for parallelizing Perl/C code. This is an area that is extremely interesting as OpenMP is a rather straightforward way to parallelize code using simple compiler pragmas in Inline::C sections of code. Furthermore, as I discussed at TPRC2024, the combination of the Many Core Engine (/var/www/users/chrisarg/index.html

Is Perl a write only language?

I am sick and tired of hearing this, so let's put it this to the test. Assume you know little of Perl, or any programming language for that matter. Can you parse the code?

https://chrisarg.github.io/Killing-It-with-PERL/2023/12/06/Is-Perl-a-write-only-language.html

I hope the piece above is the first in a series to convince people to consider the reality before passing judgement. It was inspired by one of our research analysts discovering Perl and awk to simplify their l…