r/perl • • 14d ago

Affix: FFI that makes native code feel like Perl

Affix is an FFI for Perl with a simple set of goals:

  • calling native code and dealing with raw memory should feel like writing ordinary Perl
  • it should be incredibly fast
  • the name should describe the act of binding something to something else and have 'FFI' in the dead center so you don't forget what it is

I wanted an FFI that feels good. This is what I came up with:

use v5.40;
use Affix qw[:all];
affix libc(), 'strlen', [ String ] => Size_t; # This is a terrible example but...
say strlen('hello');    # ...five.

Or maybe you're on Windows?

use Affix qw[:all];
wrap( 'user32', 'MessageBoxA', [ UInt, String, String, UInt ] => Int )->( 0, 'JAPH,', 'Hello, World', 0 );

...okay, hardly groundbreaking so far but I'm starting slow.

Affix is a bundle of amazing stuff but the type system might be the secret sauce. Primitives, enums, unions, bitfields, structs (including packed and nested structs), pointers, callbacks, variadics, wide strings, file handles, 128bit numerics, C99 complex numbers, flexible and n-dimensional arrays, and SIMD vectors are all first-class. Cast C memory and you get native Perl hashes and arrays with magic underneath, so $p->{x} = 42; writes straight into the C struct. You can even pin a scalar to an exported symbol or address in memory and read and write through it. No generated accessor methods, no copies, no ceremony.

use v5.40;
use Affix qw[:memory :types :core];
use Affix::Build;
#
my $c = Affix::Build->new;
$c->add( \<<~'C', lang => 'c' );
    typedef struct { int x; int y; } Point;
    int get_x( Point * pt ) { return pt->x; }
    int get_y( Point * pt ) { return pt->y; }
    Point new_pt( int x, int y ) { Point pt = { .x = x, .y = y }; return pt; }
    C
#
typedef Point => Struct [ x => Int, y => Int ];
affix $c->link, 'get_x', [ Pointer [ Point() ] ] => Int;
affix $c->link, 'get_y',  [ Pointer [ Point() ] ] => Int;
affix $c->link, 'new_pt', [ Int , Int ] => Point();
#
my $p = cast( malloc( sizeof Point() ), Point() );    # Allocates memory and pins a scalar to it
$p->{x} = 42;                                         # Writes directly to memory
$p->{y} = 7;
#
say get_x($p) . ' x ' . get_y($p);                    # The pin works as a pointer arg (42 x 7)
say get_x( { y => 50, x => 100 } );                   # Hashes are coerced automatically (100)
#
free $p;                                              # Auto-freed at end of scope, or freed early manually
$p->{x} = 1000;                                       # No longer bound to C memory; just a normal hashref now
#
my $p2 = new_pt(30, 25);
say $p2->{x} . ' x ' . $p2->{y};                      # struct from C is treated as a hashref (30 x 25)
say get_x($p2);                                       # Which you can turn around and hand back to native code even as a pointer (30)

In that example, I used Affix::Build which wraps compilers and build systems from Assembly to Zig to create shared libs for Affix (or even FFI::Platypus!). Think of it as Inline::C for FFI.

use v5.40;
use Affix qw[affix Int];
use Affix::Build;
#
my $c = Affix::Build->new;
$c->add( \<<~'C', lang => 'c' );
    int plus(int a, int b) { return a + b; }
    C
#
affix $c->link, 'plus', [ Int, Int ] => Int;
say plus( 2, 3 ); # ...also five.

You can even compile and link multiple languages into a single lib as long as they share similar ABIs. I'm not sure why you'd want to do that but you can.

Affix::Wrap goes the other direction: hand it C/C++ headers and it parses them using Clang's AST (with a pure-Perl fallback if you don't have LLVM installed) to generate bindings for an entire library. Bindings are ready to run immediately, or you can export them as an early draft if your project is bound for CPAN.

So, we've made native code feel like Perl but is it fast?

FFI can come with a lot of overhead but I regularly benchmark Affix against hand-written XS, Inline::C, and FFI::Platypus. Here's a quick snapshot from the latest v1.4.3 dist:

Linux x86_64 (perl v5.42.0)

  • sin(double): Affix 14.45M/s | XS 14.15M/s | Inline::C 13.31M/s | FFI::Platypus 5.32M/s
  • abs(int): Affix 11.96M/s | XS 12.24M/s | Inline::C 12.19M/s | FFI::Platypus 4.96M/s
  • strlen(const char*): Affix 11.60M/s | XS 15.35M/s | Inline::C 16.59M/s | FFI::Platypus 5.39M/s

Windows x64 (perl v5.42.3)

  • sin(double): Affix 16.60M/s | XS 5.36M/s | Inline::C 5.39M/s | FFI::Platypus 2.42M/s
  • abs(int): Affix 12.26M/s | XS 5.84M/s | Inline::C 5.89M/s | FFI::Platypus 2.30M/s
  • strlen(const char*): Affix 11.03M/s | XS 6.59M/s | Inline::C 6.59M/s | FFI::Platypus 2.41M/s

(Full multi-platform results including aarch64 and macOS are in this workflow run.)

XS can still win on very well-optimized calls, and Inline::C and FFI::Platypus are battle-tested tools that I have a lot of respect for. The point of Affix is that you don't have to trade away runtime binding, rich types, or zero-copy structs to get near-XS (or better) call throughput.

I'll try to write more about the things I'm working on. I rarely see my own projects as complete or even far enough along for a public announcement like this. To demonstrate just how silly that is, Affix has been on CPAN for five years but I've never spoken or even written about it at length outside of the project's repo. I think the projects I pick up are all cool, and it'd be nice if others thought so too, which is hard to do if no one knows they exist. My current goal for Affix is to support popular compilers on every 64bit system I can buy or find a GitHub runner for: Windows, Linux, macOS, Haiku, Solaris, Android (Termux), GNU/Hurd, and several BSDs on x64, ARM, and RISC-V so far. Part of feeling like Perl is working (when it makes sense) where Perl works.

29 Upvotes

8 comments sorted by

3

u/stratum42dev 14d ago

I can run ppc64be AIX and ppc64le linux for you. You'd only have validation for POWER8 cpus, so a bit outdated, but still a rare bird. It makes me so happy to see you went for public release too!!!!!! I currently have a Go compiled shared object linked in part of my codebase. It'll take me a couple days but I'm curious to see if your approach would work better than mine.

1

u/s_throwaway_r 13d ago

Rare for sure! It would be my JIT compiler's) first big-endian target and AIX uses a function descriptor system with a table of contents instead of function pointers. I'm just starting to investigate it as a plausible goal and it's already kinda blowing my mind.

IBM has public runners for ppc64le and s390x. The first one will get me 5% of the way to ppc64be (fixed width instructions, the instruction set itself, register handling, etc.). The remaining 95% would be AIX itself; the TOC behavior, XCOFF, aggregate layout rules (infix is my type introspection engine too), and on and on... I have a lot of reading to do.

I might have to try this out to (slowly) get things off the ground. QEMU will get me XCOFF and the ABI but doesn't help with accurate cache coherency which might bite me on real hardware.

I'll keep you in mind but it'll be way down the road for ppc64be/AIX. I'm honestly shocked it compiled according to your other comment. Haha...

2

u/stratum42dev 13d ago

DM me if you're serious about AIX development, we could work out a runner on one of my servers for you to use with some limits of course.

2

u/stratum42dev 14d ago

It's not likely super important, but I did find what I would classify as a "super-esoteric" bug. The module can't seem to build properly on AIX 7.2 TL4, but works great on ppc64le Tumbleweed. I didn't want to take away the fun of debugging and be a buzzkill, so I left it alone. The test suite starts failing on AIX at t/19.

1

u/stratum42dev 14d ago

can confirm, works well with go

3

u/vadrer 🐪 cpan author 14d ago

my first reaction was "Oh no, yet another FFI", second reaction "at last someone done it right".

Nice work! :)

2

u/saiftynet 🐪 cpan author 14d ago

This is fascinating! I am really eager to learn this; I see you have already used this to get SDL3 on Perl.

1

u/ReplacementSlight413 14d ago

Man this is great