The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Form::Tiny::Manual::Performance - validation performance tips

DESCRIPTION

Form::Tiny is a pretty fast validation framework (benchmark), but when the need arise, certain steps can be taken to improve it even further.

THINGS THAT SLOW THE MODULE DOWN

Inline forms

The module gives you an option to create inline forms - without the need to set up a dedicated form class. While the inline style does not give you full capabilities of the regular form, it also is slow to instantiate, because it has to re-create its metamodel object everytime form constructor is called. Creating a dedicated package for a form will ensure that metamodel object will only be created one time in script runtime.

Dynamic fields

Dynamic fields are fields which are built at runtime. They are created anew for every form validation. This allows almost unlimited flexibility, but comes at a price of performance.

Optional features

Form::Tiny offers a large set of optional features. There are strict mode, filters, coercions, adjustments, extra validators, hooks and many more. The more features you use, the worse performance will get - that shouldn't be a surprise to anyone. It shouldn't stop anybody from using them though.

Take strict mode as an example - while it may come useful, it is often unnecessary. The module already does not copy any extra data from input to output fields, so you don't have to worry about it at all - it will simply get ignored. Benchmarking has shown that enabling strict mode reduces the performance of the module by up to 30% (more for simple forms).

THINGS THAT SPEED THE MODULE UP

Flat forms

Form::Tiny has a built in condition that allows it to optimize form validation when the form is "flat": no fields are nested or dynamic. If you can avoid having nested fields (fields with a dot, like hashref.key or arrayref.*) or dynamic fields (fields which are constructed by a sub) in you form, validation speed will be increased by up to 70%. The module can then completely skip searching for the field in the input structure and simply use its name as a key in the input hash reference.

Class::XSAccessor

When using Form::Tiny with Moo, installing Class::XSAccessor can provide small but free boost to runtime speed of the module.

Type::Tiny::XS

Form::Tiny uses a lot of Type::Tiny type constraints, both internally and in its form classes. Installing Type::Tiny::XS should give them some XS boost, although it may be hard to measure exactly what kind of performance boost it gives.