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

AutoXS - Speed up your code after compile time

SYNOPSIS

package MyClass;
sub blah {...}
use AutoXS ':all';
# if blah matches one of the patterns, it's running
# as XS code now!

DESCRIPTION

Warning: This module contains some scary code. I'm not even sure it abides by the official Perl API totally. Furthermore, it's my first real XS module. It abuses some features of the XS/XSUB syntax. If you break it, you get to keep both halves.

That being said, the purpose of this module and its plugin modules is to speed up the execution of your program at the expense of a longer startup time. AutoXS::Accessor comes with the same distribution as an example plugin.

AuotXS plugins use the B and B::Utils modules to scan all subroutines (or methods) in the calling package for certain patterns. If a subroutine complies with such a pattern, it is replaced with an XS subroutine that has the same function.

The XS subroutines for replacement are not compiled at runtime like Inline::C would do. They have been compiled at module build time just like any other XSUBs.

In a simple minded test, AuotXS::Accessor sped up typical read-only accessors by a factor of 1.6 to a factor of 2.5. Your mileage may vary, of course. Keep in mind mind that accessors can sometimes be part of very tight loops.

To get an impression of the imposed pre-runtime penalty of using AutoXS, a file containing nine methods (code shown in AutoXS::Accessor) was compiled with and without AutoXS. The test is contrived because all nine methods will be replaced. In normal code, there is much more non-accessor code which will be quickly rejected. Naturally, rejection is faster than successful matching and replacement. The compilation with AutoXS took 74ms longer than without.

USAGE AND SYNTAX

You use this module by loading it with use. It is important to load it at compile time, so if you must absolutely separate the file loading and importing steps, do so in a BEGIN block.

By loading an AutoXS plugin module in your class, all methods in the current package will be scanned for potential replacements. You can apply all installed plugins/optimizations by loading AutoXS with the ':all' tag:

package MyClass;
use AutoXS ':all';
# code that will be optimized

Apart from that, you can also specify the plugins that should be loaded and applied:

package OtherClass;
use AutoXS plugins => ['Accessor']; # that one comes with AutoXS
# will optimize your accessors.

If you want to know which subroutines/methods were optimized, you can load AutoXS with the debug option. Note that the following call only sets the debug option, but does not apply any optimizations:

use AutoXS debug => 1;

You can also set the debug option on plugin classes, but the option is still global to all plugins:

use AutoXS::Accessor debug => 1; # same as the last example

Loading a plugin with no argument applies its optimizations:

package SomeClass;
use AutoXS::Accessor;
# optimizes accessors only.

CAVEATS

This is alpha code. Beware.

The module abuses XS syntax constructs.

The startup penalty may be big and is proportional to the number of subroutines in the calling package.

The module works its magic at CHECK time. CHECK blocks are not executed in string eval "" environments. That means this module potentially does not work in a PAR packaged executable. (It should do no harm there, either.)

SEE ALSO

AutoXS::Accessor

AutoXS::Header

B, B::Utils

Much cleverer: Faster

AUTHOR

Steffen Mueller, <smueller@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2008 by Steffen Mueller

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8 or, at your option, any later version of Perl 5 you may have available.