Introduction to SpeedyCGI

Sam Horrocks
Presented at YAPC North America
14 June 2001


This paper will introduce SpeedyCGI, a persistent perl interpreter, and will cover the following topics:

What is SpeedyCGI?

SpeedyCGI is a persistent perl interpreter. In traditional perl when you run a perl script, a new process is created, your perl script is compiled and executed, and then the perl process exits. If the same script is run again, all these steps are repeated.

SpeedyCGI behaves a little differently. Just as in regular perl, the first time a perl script is run, a new process is created and the script is compiled and executed. However, with SpeedyCGI, at this point instead of exiting, the perl process is retained. If the same script is run again, then the perl process can execute it right away, without re-reading and re-compiling the script.

Why was SpeedyCGI created?

SpeedyCGI was created because a solution was needed that would: There wasn't anything at the time that would meet all these requirements, so SpeedyCGI was created.

How do I use SpeedyCGI?

The simplest way is to change the #!/usr/bin/perl line at the top of your script to #!/usr/bin/speedy. For higher performance, you can also run SpeedyCGI via an Apache module (mod_speedycgi).

Unfortunately not all code works correctly when run persistently. So in addition you may have to clean up your code to make it work. Using strict and the -w switch will solve a lot of these problems.

Why would I use SpeedyCGI instead of normal perl?

Performance. Under SpeedyCGI, if you run the same perl code over and over the script doesn't have to be compiled each time it is run. This means less CPU time is required for each run.

In addition, once your code is running persistently, it's possible to speed up execution even further by caching data or objects in global variables. In SpeedyCGI, global variables retain their values between runs of your script. You can take advantage of this fact to cache things like database handles or other resources instead of having to re-initialize them each time the script is run.

For example, if you have a subroutine "get_db_handle" that returns a database handle, the following code will cache this handle in a persistent global variable:

    use vars '$dbh';
    $dbh ||= &get_db_handle;
The first time this code is run, $dbh will be undefined so the || operator will cause get_db_handle to be called. On subsequent runs of the script, $dbh will already be defined (it will already hold the database handle) and get_db_handle will not be called.

Comparison to other persistent perl interpreters

There are other persistent perl environments around. Below is a comparison between SpeedyCGI and two popular persistent perl interpreters - mod_perl and FastCGI.

Comparison to both mod_perl and FastCGI

mod_perl Comparison

FastCGI Comparison

How does SpeedyCGI work?

When you run /usr/bin/speedy, you are not directly running a perl interpreter. The speedy executable is only a "frontend". The actual perl interpreter is contained in a different executable named "speedy_backend".

When executed, the speedy frontend does the following:

  1. Looks for an available backend to run this script.

  2. If no backends are available, starts a new one.

  3. As soon as a backend is located, connects to it and starts to send over %ENV, @ARGV and the STDIN data.

  4. Brings back the STDOUT and STDERR data from the backend and sends it to its output.

The speedy backend does the following:

  1. Initializes the perl interpreter and compiles the perl script

  2. Waits for a frontend to contact it

  3. Once a frontend contacts it, reads in and initializes %ENV and @ARGV

  4. Sets up STDIN, STDOUT and STDERR so that they are connected to the frontend via Unix sockets.

  5. Executes the perl code.

  6. Goes back and waits for another frontend to contact it.

A data file in /tmp is used to keep track of the frontends and backends. Once the two processes find each other, Unix sockets are used for communication.

Future Directions

More Information

For more information about SpeedyCGI see the SpeedyCGI home page at