One goal we have is making Dancer2 a fast web framework. While we continue to optimize our code, some changes are not possible due to obligations we have to users. However, there are many actions you can take to optimize your Dancer2 web applications.
In this series of articles we will explore these actions, in order of the involvement required from you, starting from installing modules to configuring your server, to changing your architecture and code.
While some of these suggestions apply to Dancer2, others apply to other frameworks, and even other languages.
In this article we focus on optimizations that are built into Dancer2, but require your involvement. Specifically, this relates to various modules that, if you install them, things will simply run much faster.
The first question, before explaining what you can install, is why. Why can't all of these modules simply be part of Dancer2? In short, users.
Dancer2 is used by many users, in various environments, all with different considerations. Some users install it on their machine, while others might use a hosting company.
Dancer2 provides a promise to always use Pure-Perl code. This means that we will not, by default, use modules that require a C (or C++) compiler in order to install. This means you can always advent-calendar-on-fatpack your application with App::FatPacker.
Dancer2 will then check whether certain modules are available, and if so, will use those modules instead of the Pure-Perl default implementation. There are various other modules which take the same approach, meaning they in turn will also run faster having certain modules available.
We try to list as many as possible of these modules in the documentation and in the metadata for installers, so some might be installed when you install Dancer2. Still, you might want to double check you have them installed.
Currently Dancer2 uses URL::Encode to handle the URL encoding. While we intend to move this coming year to URL::XSEncode, we haven't done so yet. Until we do, we will check whether you have the XS version installed, and if so, we will use that.
No, we don't use CGI, don't you worry. However, we do parse the query string with this fast module.
Dancer2 uses several components of the Plack set of utilities. It itself uses this module if it is available, so we recommend installing it.
We have moved our code from JSON to JSON::MaybeXS. This makes the Dancer2 automatically try and use an XS JSON parser (preferably Cpanel::JSON::XS). While it might load JSON::XS, we recommend Cpanel::JSON::XS instead.
- Math::Random::ISAAC::XS and Crypt::URandom
If both of these modules are available, Dancer2 will generate a secure session ID. For security and speed purposes, we recommend installing them.
Have you ever noticed that
passreturn from the route for you? We achieve this by using a module called Return::MultiLevel. It has a Pure-Perl implementation, but if the module Scope::Upper is installed, it will use that instead.
- EV (in case of AnyEvent)
Dancer2 has asynchronous support, allowing for asynchronous code and streaming responses. If you are writing an asynchronous web application using AnyEvent or run on an AnyEvent server, we recommend you install EV.
In the next article, we will explore what kind of server configurations we can apply in order to achieve better performance for our applications.
This article has been written by Sawyer X for the Perl Dancer Advent Calendar 2016.
No copyright retained. Enjoy.