How PerlEx Works

The PerlEx Precompiler

When your web server receives its first request to execute a Perl script, PerlEx is loaded. PerlEx then runs its precompiler on the Perl script, which executes any operations within BEGIN blocks or use statements, compiles the main body of the Perl script, and retains the compiled state of the script in memory. The compiled state of the script is then executed by PerlEx.

The second time that the same Perl script is requested, PerlEx simply executes the compiled state of the script which is memory-resident - it is not necessary to go through the compilation phase again. This accounts for much of the performance increase PerlEx has over standard CGI Perl scripts.

Note that the first execution of any PerlEx script will not be as fast as those that follow, as the script will be compiled by the PerlEx precompiler the first time it is requested.

When the total number of times an individual Perl interpreter has been used to execute scripts reaches the number defined by the Reload registry entry, all of the END blocks will be run for each precompiled script, the compiled state of the script will be released from memory, and the Perl interpreter will be reinitialized. Reloading interpreters ensures that arbitrary Perl scripts that might accumulate small amounts of memory for each request do not eventually make the system run out of memory. If you are sure your scripts do not have such memory leaks, you could increase this value, though the benefit of doing so will usually be negligible.

Threading Behavior

Normally, PerlEx runs scripts in the same thread of execution that called into it. Such threads are created and managed by the web server, and their numbers are determined by the web server based on available resources and current load on the server. However, if the MinThreads registry option is set to a particular number greater than 0, PerlEx will create its own pool of threads to process incoming requests. Under this scenario, the incoming web server thread can return immediately while PerlEx is processing the request. PerlEx will subsequently notify the web server when it has finished processing the request, and is ready to hand the response back to the web server. Making PerlEx use its own pool of threads in this fashion can be advantageous in many situations, most notably those involving COM objects that are capable of executing in the Multi-Threaded Apartment (MTA) thread model.

PerlEx can dynamically alter the number of threads automatically based on prevailing demand and load conditions; if the number of requests exceeds the MinThreads setting and the CPUs have spare capacity, PerlEx will create additional threads to service these requests. The total number of such threads is only limited by the total number of Interpreters configured. Any additional requests over and above the available number of threads will be queued up and processed in the order they were received, as threads become available.

Interpreter Classes

The run time characteristics of PerlEx can be controlled by specifying parameters that apply to a group of interpreters that must be used to serve one or more scripts. A set of such parameters is referred to in this document as an Interpreter Class. The Interpreters option in an Interpreter Class specifies how many Perl interpreters are exclusively devoted to serving scripts in that Interpreter Class. When there are more requests for script execution than there are interpreters in a particular Interpreter Class, PerlEx will make the additional requests wait until interpreters become available.

An Interpreter Class with the Interpreters option set to 0 will not precompile any scripts found matching its ScriptLocation setting. This is similar to the functionality of the Noload registry entry in older versions of PerlEx, and closely matches the behavior of regular CGI scripts, and scripts run under Perl for ISAPI. Enabling this option disables many of the performance benefits of PerlEx over CGI, but it can be useful for Perl scripts that cannot run properly with PerlEx, due to various coding issues that must be considered in the PerlEx environment.

For more information, please see:

PerlEx Interpreter Classes

WebServer Configuration

BEGIN and END blocks

Embedding Perl in HTML files

PerlEx Precompiler

Registry Entries

Reload & ReloadAll functions