perlperl-xs

When should you use XS?


I am writing up a talk on XS and I need to know when the community thinks it is proper to reach for XS.


Solution

  • I can think of at least three reasons to use XS:

    1. You have a C library you want to access in Perl 5
    2. You have a block of code that is provably slowing down your program and it would be faster if written in C
    3. You need access to something only available in XS

    Reason 1 is obvious and should need no explaination.

    When you really need reason 2 is less obvious. Often you are better off looking at how the code is structured. You should only invoke reason 2 if you have profiled your code and have a benchmark and test suite to prove that the XS code is faster and correct.

    Reason 3 is a dangerous reason. It is rare that you actually need to look into Perl's guts to do something, but there is at least one valid case.