Optimizing Dancer2 PT. 1
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.
Built-in optimizations
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.
Why can't it just happen automatically?
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.
What should I install?
- Class::XSAccessor
Dancer2 uses Moo as the object system internally. Moo itself can be sped up considerably by having this module installed.
- URL::Encode::XS
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.
- CGI::Deurl::XS
No, we don't use CGI, don't you worry. However, we do parse the query string with this fast module.
- HTTP::Parser::XS
Dancer2 uses several components of the Plack set of utilities. It itself uses this module if it is available, so we recommend installing it.
- YAML::XS
The Dancer2 configuration is read by Config::Any, in order to allow you to use any format you prefer. Config::Any will prefer YAML::XS, if it's available.
- Cpanel::JSON::XS
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.
- HTTP::XSCookies
There are several cookie parsing modules on CPAN. There is an XS version, but it doesn't handle everything. Instead, and because of Dancer2, HTTP::XSCookies was written. Dancer2 will use it, if available. Do you use cookies or sessions? You should install it.
- 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.
- Scope::Upper
Have you ever noticed that
redirect
,forward
,send_error
, andpass
return 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.
Coming next
In the next article, we will explore what kind of server configurations we can apply in order to achieve better performance for our applications.
Author
This article has been written by Sawyer X for the Perl Dancer Advent Calendar 2016.
Copyright
No copyright retained. Enjoy.
Sawyer X.