Perl XS and Inline::C
Asked Answered
I

2

13

What's the difference between using XS and the Inline::C module? This was mentioned by someone in this question and has made me curious.

Internode answered 29/7, 2011 at 20:41 Comment(0)
S
14

Inline::C generates XS and builds the generated module. It does this at run-time, although it caches past builds.

Inline::C is possibly easier to use, but there are a few downsides. The first time it runs, it slows down startup, it requires permissions to create files at run-time, and it requires the tools to compile the module. Furthermore, it makes it harder for a sysadmin to install.

The upside is that you can grab the generated XS and eliminate Inline::C once things start shaping up. This makes it useful for prototyping.

Snowdrop answered 29/7, 2011 at 21:10 Comment(2)
The hard part of "XS" is understanding the perl API. And you're going to have to do that whether you use XS or Inline::C.Zobe
Cf. the experimental InlineX::XS for a way to use Inline::C for prototyping and XS for deployment.Fricandeau
P
3

Inline compiles the C code at the same time when your Perl is compiled, and will be recompiled every time your source code is changed. XS is compiled once and the binary is saved as a .so file like a library.

Perl is written in C so XS uses the native Perl types and subroutine mechanisms. A module using XS runs almost as efficiently as a built-in language feature. It's more difficult to do some things in Inline, and there will be a conversion step when calling or returning from your code. That being said, Inline does a good job of not recompiling when not necessary, and the conversions into and out of Inline code aren't likely to be a noticeable performance hit.

Finally, writing XS assumes that you are packaging a module. There is a lot of setup and knowledge of Perl guts and module packaging required. If you just need to call a C library from Perl, you are better off using Inline.

Poeticize answered 29/7, 2011 at 21:31 Comment(2)
Re "will be recompiled every time your source code is changed", that's a good thing. you still need to recompile when the source code changes when using XS directly, but it's not automatic then.Snowdrop
Re "and there will be a conversion step when calling or returning from your code." The same conversion step necessarily exists for both XS and Inline::C. In fact, Inline::C lets XS do all the converting.Snowdrop

© 2022 - 2024 — McMap. All rights reserved.