Practical FFI with Platypus
At YAPC::NA 2014 I talked about FFI and Perl. FFI is an alternative to XS that I think is worthy of consideration. My talk was well attended, I think primarily because I jokingly subtitled my talk "Never Need To Write XS Again". So there is a market for this idea. I mostly talked about FFI::Raw, which was a great way to experiment with FFI and to write real live CPAN modules with FFI right then and there. The question of performance inevitably came up, so at the Pittsburgh Perl Workshop last year I talked about that.
Essentially I think the performance gains you get from a well tuned XS extension are not always justified by the amount of time that you spend debugging them. I also think that FFI more easily ports your skills to other technologies and lends itself more easily to a bright future for Perl in which there are multiple implementations, some of which do not need to support XS. All that being said, there were some tweaks that could be made in order to make FFI faster on Perl, so I wrote a prototype code named Platypus to experiment with that idea. At Pittsburgh I showed off the performance benchmarks for my prototype, which were very good in comparison to FFI::Raw and not terrible in comparison to XS and Inline::C.
I've now reworked FFI::Platypus and released it to CPAN. Feedback is welcome. Very briefly benefits include:
- Practical bindings to libffi to write stand alone scripts and CPAN modules today in 2015
- Faster invocation through real xsubs rather than object oriented method dispatch
- Support for types not directly provided by libffi like pointers and arrays
- API for creating custom types (an analogue to typemaps in XS)
There are a number of examples included with the FFI::Platypus distribution. Check it out if I have piqued your interest. Constructive feedback and pull requests are welcome.